到 phpmyadmin 的致命异常 AsyncTask 连接

Posted

技术标签:

【中文标题】到 phpmyadmin 的致命异常 AsyncTask 连接【英文标题】:Fatal exception AsyncTask connection to phpmyadmin 【发布时间】:2014-10-18 20:40:41 【问题描述】:

我正在尝试登录与phpmyadmin 连接的应用程序。当我启动应用程序时出现异常。

这是我的代码:

public class MainActivity extends Activity 

// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText fname;
EditText lname;
EditText username;
EditText password;
EditText location;
EditText contact;

Button btnreg;
Button btncancel;

// url to create new product
private static String url_new_user = "http://192.168.0.1XX/android_test/newUser.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Edit Text
    fname = (EditText) findViewById(R.id.fname);
    lname = (EditText) findViewById(R.id.lname);
    username = (EditText) findViewById(R.id.uname);
    password = (EditText) findViewById(R.id.pass);
    location = (EditText) findViewById(R.id.addr);
    contact = (EditText) findViewById(R.id.contact);


    // Create button
    btnreg = (Button) findViewById(R.id.btnreg);
    btncancel = (Button) findViewById(R.id.btncancel);

    // button click event
    btnreg.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View view) 
            // creating new product in background thread
            new CreateNewProduct().execute();
        
    );

    btncancel.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View view) 
            finish();
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
        
    );


/**
 * Background Async Task to Create new product
 * */
class CreateNewProduct extends AsyncTask<String, String, String> 

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() 
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Registering New User..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) 
        String Firstname = fname.getText().toString();
        String Lastname = lname.getText().toString();
        String Username = username.getText().toString();
        String Password = password.getText().toString();
        String Address = location.getText().toString();
        String Contact = contact.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("fname", Firstname));
        params.add(new BasicNameValuePair("lname", Lastname));
        params.add(new BasicNameValuePair("username", Username));
        params.add(new BasicNameValuePair("password", Password));
        params.add(new BasicNameValuePair("location", Address));
        params.add(new BasicNameValuePair("contact", Contact));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_new_user,
                "POST", params);

        // check log cat from response
        Log.d("Create Response", json.toString());

        // check for success tag
        try 
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) 
                // successfully created a user
                Intent i = new Intent(getApplicationContext(), RegActivity.class);
                startActivity(i);

                // closing this screen
                finish();
             else 
                // failed to create user
                Log.d("failed to create user", json.toString());

            
         catch (JSONException e) 
            e.printStackTrace();
        

        return null;
    

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) 
        // dismiss the dialog once done
        pDialog.dismiss();
    


这是logcat: 08-25 16:48:30.499:E/JSON Parser(1410):解析数据时出错 org.json.JSONException:值

【问题讨论】:

【参考方案1】:

改为:

protected String doInBackground(String... args) 
    ...
    if (success == 1) 
        return "hooray";
    
    ...


protected void onPostExecute(String file_url) 
    // dismiss the dialog once done
    pDialog.dismiss();
    if (file_url != null) 
        // successfully created a user
        Intent i = new Intent(getApplicationContext(), RegActivity.class);
        startActivity(i);
        // closing this screen
        finish();
    

【讨论】:

@Aleksander 你连接到互联网了吗?是否添加了互联网权限? 是的,我已连接并添加了 Internet 权限,我忘记了 exeption 也在显示----- 08-25 16:48:30.499: E/JSON Parser(1410): Error parsing data org. json.JSONException:Java.lang.String 类型的值
@Aleksander 手动输入网址并检查浏览器中的输出。似乎它要么向您显示错误,要么有一些 html 代码无法被 JSON 解析(即 )。 浏览器中的输出是 "success":0,"message":"Required field(s) is missing"。 ------我不知道你在尝试将android与phpmyadmin连接3天。 @Aleksander 那么问题出在 php 中。请再问一个问题,因为这似乎与您的问题无关。

以上是关于到 phpmyadmin 的致命异常 AsyncTask 连接的主要内容,如果未能解决你的问题,请参考以下文章

xampp phpmyadmin 致命错误

致命错误:phpMyAdmin 中的执行时间超过 30 秒

致命错误:内存不足 Zend 错误 / PhpMyAdmin

phpMyAdmin:PHP 致命错误 - 无法重新声明 PMA_checkLink()

致命错误:未捕获的类型错误:参数 1 传递给 PhpMyAdmin\UserPreferences::apply()

PHPMyAdmin 致命错误 - Twig_Error_Loader