因此我使用androidhive教程爲我的應用程序創建服務器並連接到它。我有它,所以服務器會根據發送的內容發回不同的消息,但我得到一個錯誤,我不明白爲什麼。下面是在發生錯誤的類:應用程序在嘗試報告錯誤時出現錯誤
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();
}
}
現在,我在我的數據發送故意讓成功== 2,但它告訴我,我的應用程序發生意外錯誤。爲什麼是這樣?是否因爲pDialog仍然開放?我試着把pDialog.dismiss();上面但我仍然得到錯誤。對不起,如果這是一個簡單的問題,並提前謝謝你。
泰勒
編輯: 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();
}
}
您能否將錯誤消息添加到您的問題?如果這是一個應用程序崩潰,然後看看你的應用程序的logcat輸出。如果您使用Eclipse進行開發,您可以在DDMS透視圖中找到它。它應該是一堆以「未捕獲的異常」開頭的紅色日誌行。 – tiguchi
@Tylerm檢查我的答案。 –