如何處理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敬酒不顯示
而你的問題是......? – Henry
如何使用吐司顯示網絡服務錯誤 –