Reputation: 151
so I'm using the androidhive tutorial to make a server for my app and connect to it. I have it so the server will send back different messages depending on what was sent in but I'm getting an error with it and I can't figure out why. Here is the class that the error occurs in:
class CreateNewSpot extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewSpotActivity.this);
pDialog.setMessage("Creating Spot..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String longitude = inputLong;
String latitude = inputLat;
String pavement = spinner_pavement.getSelectedItem().toString();
String traffic = spinner_traffic.getSelectedItem().toString();
String environment = spinner_enviro.getSelectedItem().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("longitude", longitude));
params.add(new BasicNameValuePair("latitude", latitude));
params.add(new BasicNameValuePair("pavement", pavement));
params.add(new BasicNameValuePair("traffic", traffic));
params.add(new BasicNameValuePair("environment", environment));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
switch(success){
case 0:
//name is empty!
break;
case 1:
// successfully created product
Intent i = new Intent(getApplicationContext(),
AllSpotsActivity.class);
startActivity(i);
// closing this screen
finish();
break;
case 2:
//name has been taken
Toast.makeText(getApplicationContext(), "Name for spot has already been taken.", Toast.LENGTH_LONG).show();
break;
case 3:
//server error
Toast.makeText(getApplicationContext(), "A server error has occurred.", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getApplicationContext(), "An unknown error has occurred.", Toast.LENGTH_LONG).show();
//just an unknown error
break;
}
} 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();
}
}
Now I'm purposely sending in data to get success==2 but it tells me my app unexpected error has occurred. Why is this? Is it because of the pDialog is still open? I tried putting pDialog.dismiss(); above but I still get the error. Sorry if this is a simple question and thank you in advance.
Tyler
EDIT: Logcat:
class CreateNewSpot extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewSpotActivity.this);
pDialog.setMessage("Creating Spot..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String longitude = inputLong;
String latitude = inputLat;
String pavement = spinner_pavement.getSelectedItem().toString();
String traffic = spinner_traffic.getSelectedItem().toString();
String environment = spinner_enviro.getSelectedItem().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("longitude", longitude));
params.add(new BasicNameValuePair("latitude", latitude));
params.add(new BasicNameValuePair("pavement", pavement));
params.add(new BasicNameValuePair("traffic", traffic));
params.add(new BasicNameValuePair("environment", environment));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
switch(success){
case 0:
//name is empty!
break;
case 1:
// successfully created product
Intent i = new Intent(getApplicationContext(),
AllSpotsActivity.class);
startActivity(i);
// closing this screen
finish();
break;
case 2:
//name has been taken
error_msg = 0;
break;
case 3:
//server error
error_msg = 1;
break;
default:
error_msg = 2;
//just an unknown error
break;
}
} 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
switch(error_msg){
case 0:
Toast.makeText(getApplicationContext(), "Name for spot has already been taken.", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "A server error has occurred.", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "An unknown error has occurred.", Toast.LENGTH_LONG).show();
break;
default:
break;
}
pDialog.dismiss();
}
}
Upvotes: 0
Views: 64
Reputation: 1422
You are getting unexpected error because you are showing Toast from doInBackground(), which you can't do. You never handle your UI from background in AsyncTask. Just remove your try-catch block from doInBackground() to onPostExecute() and it will work.
Upvotes: 2