我试图根据json结果显示吐司,所以当json结果等于2时,我想向用户显示他应该再次尝试而不离开当前活动,但应用程序被强制关闭时json响应是2,这是我的代码:
class NewAccount extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Register.this);
pDialog.setMessage("Registering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
*/
protected String doInBackground(String... args) {
String name = iname.getText().toString();
String email = iemail.getText().toString();
String reEmail = ireEmail.getText().toString();
String password = ipassword.getText().toString();
String rePassword = irePassword.getText().toString();
String id = iid.getText().toString();
String phone = iphone.getText().toString();
String address1 = iaddress1.getText().toString();
String address2 = iaddress2.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("id", id));
params.add(new BasicNameValuePair("phone", phone));
params.add(new BasicNameValuePair("address1", address1));
params.add(new BasicNameValuePair("address2", address2));
System.out.print(name.length());
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_register,
"POST", params);
if(json != null) {
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(Register.this, MainPage.class);
startActivity(i);
// closing this screen
finish();
}
else if(success == 2){
Toast.makeText(getApplicationContext(), "kejfkjerfk", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
else{
Toast.makeText(getApplicationContext(), "An error occurred, please try again", Toast.LENGTH_LONG).show();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
答案 0 :(得分:1)
你应该做一个在成功== 1:
时完成的循环while (true){
...
if (success == 1) {
// successfully created product
Intent i = new Intent(Register.this, MainPage.class);
startActivity(i);
// closing this screen
finish();
break;
}
else if(success == 2){
Toast.makeText(getApplicationContext(), "kejfkjerfk", Toast.LENGTH_LONG).show();
}
...
}