我应该如何处理服务器超时和错误code反应,在Android应用程序的HTTP POST? [英] How should I handle server timeouts and error code responses to an http post in Android App?

查看:130
本文介绍了我应该如何处理服务器超时和错误code反应,在Android应用程序的HTTP POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序做HTTP职位,如 http://example.com/abc.php网址? email=abc@xyz.com 因此,Android应用程序基本上会谈,在服务器端PHP的和接收JSON响应,并分析它们来填充应用程序的各种意见。正常工作。

My Android App does http posts to URLs like http://example.com/abc.php?email=abc@xyz.com 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.

我的问题是 - 我该如何处理以下的Andr​​oid应用程序事件,这样的情况下,这些事件发生在服务器端应用程序,在App不应强制关闭象现在这样。

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.

  1. 服务器超时情况,并且没有得到任何回应。该应用程序的力量现在关闭。我想恰当地处理这个问题。

  1. Server Time out occurs and no response is received. The App force closes now. I want to handle this appropriately.

返回的错误codeS为到App HTTP POST到服务器的响应。该应用程序强制关闭,目前,因为我没有这样处理。

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.

我也遇到过这两个场景的应用程序是不是codeD来处理这些事件。请随时在Android应用程序中添加可能发生的可能导致ANRS任何其它事件发生。

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.

一个小code段或线索,将帮助我很多,因为我有这个以前从来没有这样做过。

A little code snippet or clue will help me a lot since I have never done this before.

感谢。

推荐答案

添加到很好的建议,到目前为止...

Adding to the very good suggestions so far...

我的工作助理教我用的类从org.apache.http包是这样的:

My work associate taught me to use classes from the org.apache.http package like this:

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.

这篇关于我应该如何处理服务器超时和错误code反应,在Android应用程序的HTTP POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆