2014-01-19 196 views
0

因此我使用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(); 

    } 
} 
+0

您能否將錯誤消息添加到您的問題?如果這是一個應用程序崩潰,然後看看你的應用程序的logcat輸出。如果您使用Eclipse進行開發,您可以在DDMS透視圖中找到它。它應該是一堆以「未捕獲的異常」開頭的紅色日誌行。 – tiguchi

+0

@Tylerm檢查我的答案。 –

回答

2

你得到意外錯誤,因爲你是顯示從doInBackground(),你不能做吐司。你永遠不會在AsyncTask中從後臺處理你的UI。只要從doInBackground()onPostExecute()刪除您的try-catch塊,它就會工作。

+0

當我把try-catch放在onPostExecute()時,它希望它在doInBackground()中返回;並說無法訪問的catch和強調JSONException e。我可以這樣做,所以錯誤更改全局變量,然後在onPostExecute它執行toast命令? – TylerM

+0

你是如何做到這一點,把你的更新代碼。 –

+0

我將它添加到原始帖子中。有用。 – TylerM