2013-08-24 71 views
0

如何處理Web服務的錯誤,如500或404,並顯示他們的Android舉杯內,這裏是我的一些下面的代碼:如何使用舉杯顯示Web服務的錯誤(安卓)

public String readJSONFeed(String URL) { 

    StringBuilder stringBuilder = new StringBuilder(); 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(URL); 

    try { 
     HttpResponse response = httpClient.execute(httpGet); 
     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 

     if (statusCode == 200) { 
      HttpEntity entity = response.getEntity(); 
      InputStream inputStream = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
      String line; 

      while ((line = reader.readLine()) != null) { 
       stringBuilder.append(line); 
      } 

      inputStream.close(); 
     } else if (statusCode == 500) { 

         Toast.makeText(getBaseContext(), 
            "JSON - Failed to download file", 
            Toast.LENGTH_LONG).show(); 

     } 

    } catch (Exception e) { 
     Log.d("readJSONFeed", e.getLocalizedMessage()); 
    } 
    return stringBuilder.toString(); 
} 

一旦我已經運行該代碼,並遇到一個錯誤,如500敬酒不顯示

+0

而你的問題是......? – Henry

+0

如何使用吐司顯示網絡服務錯誤 –

回答

0
runOnUiThread(new Runnable() { 
    public void run() { 
    Toast.makeText(getBaseContext(), "JSON - Failed to download file", Toast.LENGTH_LONG).show(); 
    } 
}); 

已解決問題

0

只需追加UR狀態代碼在祝酒就是所有

Toast.makeText(getBaseContext(),"JSON - Failed to download file"+status_code, 
            Toast.LENGTH_LONG).show(); 
+0

你越來越和erros?在logcat? – KOTIOS

+0

你是什麼意思 –