您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

我应如何处理服务器超时和对Android App中的http帖子的错误代码响应?

我应如何处理服务器超时和对Android App中的http帖子的错误代码响应?

到目前为止还添加了非常好的建议…

我的工作伙伴教我如何使用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.
其他 2022/1/1 18:29:01 有362人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶