当我从数据库中获取数据时,它被取出。但是当我一次又一次地显示错误时

时间:2015-10-05 12:10:09

标签: android json

当我从数据库中获取数据时,它被取出。但是当我一次又一次地显示错误时。

抛出的异常是:

org.apache.http.NoHttpResponseException: The target server failed to respond 10-05 17:52:19.653: 
E/Fail 4(626): java.io.IOException: Attempted read on closed stream. 10-05 17:52:19.653: 
E/Fail 3(626): org.json.JSONException: No value for discountOfferPrice 10-05 17:52:20.739: 
E/Fail 3(626): org.apache.http.NoHttpResponseException: The target server failed to respond 10-05 17:52:20.739: 
E/Fail 4(626): java.io.IOException: Attempted read on closed stream.

我怎样才能在android中解决这类问题。

1 个答案:

答案 0 :(得分:0)

您的服务器可能会关闭其末端的连接,但您的HttpClient无法对该事件作出反应。 当连接关闭时,抛出NoHttpResponseException。

您可以尝试覆盖setHttpRequestRetryHandler:

yourHttpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, 
                            HttpContext context) {
    if (executionCount > yourMaxRetries) {
       //Do something
         return false;
    }
    if (exception instanceof org.apache.http.NoHttpResponseException) {
        //This is your exception
          return true;
    }
    return false;
  }

});