java 我应该如何处理对 Android 应用程序中的 http 帖子的服务器超时和错误代码响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5941515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How should I handle server timeouts and error code responses to an http post in Android App?
提问by Aakash
My Android App does http posts to URLs like http://example.com/[email protected]So the Android App basically talks to PHPs on the server side and receives JSON responses and parses them to populate various views in the App. Works fine.
我的 Android 应用程序将 http 发布到诸如http://example.com/[email protected]之类的 URL因此 Android 应用程序基本上与服务器端的 PHP 对话并接收 JSON 响应并解析它们以填充各种应用程序中的视图。工作正常。
My question is- How should I handle events below in the Android App so that in case these events occur in the server side application, the App should not FORCE CLOSE as it does now.
我的问题是 - 我应该如何处理 Android 应用程序中的以下事件,以便万一这些事件发生在服务器端应用程序中,该应用程序不应像现在那样强制关闭。
Server Time out occurs and no response is received. The App force closes now. I want to handle this appropriately.
Error Codes returned as a response to the App http post to the server. The App Force closes currently since I have not handled this.
发生服务器超时且未收到响应。App force 现已关闭。我想适当地处理这个问题。
作为对应用程序 http 发布到服务器的响应返回的错误代码。App Force 目前关闭,因为我还没有处理过这个问题。
I have encountered these two scenarios where the App is not coded to handle these events. Please feel free to add any other events that might occur that might lead ANRs occur in Android App.
我遇到过这两种情况,其中应用程序未编码来处理这些事件。请随时添加可能导致 Android 应用程序中发生 ANR 的任何其他事件。
A little code snippet or clue will help me a lot since I have never done this before.
一点代码片段或线索会对我有很大帮助,因为我以前从未这样做过。
Thanks.
谢谢。
回答by Thane Anthem
Adding to the very good suggestions so far...
到目前为止,添加到非常好的建议中...
My work associate taught me to use classes from the org.apache.http package like this:
我的同事教我使用 org.apache.http 包中的类,如下所示:
String result = null;
HttpGet request = new HttpGet(some_uri);
// As Jeff Sharkey does in the android-sky example,
// use request.setHeader to optionally set the User-Agent header.
HttpParams httpParams = new BasicHttpParams();
int some_reasonable_timeout = (int) (30 * DateUtils.SECOND_IN_MILLIS);
HttpConnectionParams.setConnectionTimeout(httpParams, some_reasonable_timeout);
HttpConnectionParams.setSoTimeout(httpParams, some_reasonable_timeout);
HttpClient client = new DefaultHttpClient(httpParams);
try
{
HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() == HttpStatus.SC_OK)
{
ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = responseHandler.handleResponse(response);
}
else
{
// Do something else, if wanted.
}
}
catch (ClientProtocolException e)
{
Log.e(LOG_TAG, "HTTP Error", e);
// Do something else, if wanted.
}
catch (IOException e)
{
Log.e(LOG_TAG, "Connection Error", e);
// Do something else, if wanted.
}
finally
{
client.getConnectionManager().shutdown();
}
// Further parse result, which may well be JSON.
回答by Aleadam
As Kaj said in the comment, one option is to catch the exception, and perhaps retry or post a failure message.
正如 Kaj 在评论中所说,一种选择是捕获异常,然后重试或发布失败消息。
For response codes != 200, just read them an do your job inside an if (con.getResponseCode() == 200)
block
对于响应代码 != 200,只需阅读它们并在if (con.getResponseCode() == 200)
块内完成您的工作
Another alternative option, is to use the much nicer Apache classes:
另一种选择是使用更好的 Apache 类:
http://developer.android.com/reference/org/apache/http/package-summary.html
http://developer.android.com/reference/org/apache/http/package-summary.html
For a snippet, check BalusC's tutorial here in SO: Using java.net.URLConnection to fire and handle HTTP requests
对于片段,请在 SO 中查看 BalusC 的教程:使用 java.net.URLConnection 来触发和处理 HTTP 请求
回答by Ben Williams
You should also make sure you're not doing lengthy tasks like HTTP requests in the UI thread.
您还应该确保您没有在 UI 线程中执行诸如 HTTP 请求之类的冗长任务。