与phpmyadmin的致命异常AsyncTask连接

时间:2014-08-25 16:25:22

标签: java php android android-asynctask phpmyadmin

我尝试在与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:Value

1 个答案:

答案 0 :(得分:0)

更改为:

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();
    }
}