HttpResponse.getEntity()NetworkOnMainThreadException

时间:2012-11-20 06:13:53

标签: android networkonmainthread

  

可能重复:
  NetworkOnMainThreadException

很长一段时间以来,我一直在使用在AsyncTask中执行http请求的通用代码。 AsyncTask会返回HttpResponse个对象。一切都很好,GUI线程从未冻结或任何东西。

现在,突然间,这会创建一个NetworkOnMainThreadException

serverResponse.getEntity().getContent();

到底是什么?为什么getEntity()被认为是网络?在我看来,该行只是将响应转换为输入流,不应该需要网络连接。谁做出了这个决定?为什么他们认为这应该是网络?

异步任务:

public class AsyncHttpTask extends AsyncTask<HttpRequestInfo, Integer, HttpRequestInfo> {

public AsyncHttpTask() {
    super();
}

protected HttpRequestInfo doInBackground(HttpRequestInfo... params) {
    HttpRequestInfo rinfo = params[0];
    try{
        HttpClient client = new DefaultHttpClient();
        HttpResponse resp = client.execute(rinfo.getRequest());
        rinfo.setResponse(resp);
    }
    catch (Exception e) {
        rinfo.setException(e);
    }
    return rinfo;
}

@Override
protected void onPostExecute(HttpRequestInfo rinfo) {
    super.onPostExecute(rinfo);
    rinfo.requestFinished();
}   

回调界面:

    public interface HttpCallback {

        public void onResponse(HttpResponse serverResponse);
        public void onError(Exception e);

    }

HttpRequestInfo:

public class HttpRequestInfo {

    private HttpUriRequest request_;
    private HttpCallback callback_;
    private Exception exception_;
    private HttpResponse response_;

    public HttpRequestInfo(HttpUriRequest request, HttpCallback callback) {
        super();
        request_ = request;
        callback_ = callback;
    }

    public HttpUriRequest getRequest() {
        return request_;
    }

    public void setRequest(HttpUriRequest request) {
        request_ = request;
    }

    public HttpCallback getCallback() {
        return callback_;
    }

    public void setCallback(HttpCallback callback) {
        callback_ = callback;
    }

    public Exception getException() {
        return exception_;
    }

    public void setException(Exception exception) {
        exception_ = exception;
    }

    public HttpResponse getResponse() {
        return response_;
    }

    public void setResponse(HttpResponse response) {
        response_ = response;
    }

    public void requestFinished(){
        if(exception_ != null){
            callback_.onError(exception_);
        }
        else {
            callback_.onResponse(response_);
        }
    }
}

然后我使用jackson将json响应转换为对象。这就是异常发生的地方:

@Override
public <T> T handleResponse(HttpResponse serverResponse, Class<T> typeOfResponse) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    T responseObject = null;
    try {
        responseObject = mapper.readValue(serverResponse.getEntity().getContent(),typeOfResponse); //THIS LINE IS EVIL
    } catch (JsonParseException e) {
        throw new ARException("Couldn't handle the response because the http response contained malformed json.",e);
    } catch (JsonMappingException e) {
        throw new ARException("Mapping the json response to the response object " + typeOfResponse + " failed.",e);
    } catch (IllegalStateException e) {
        throw new ARException("Couldn't convert the http response to an inputstream because of illegal state.",e);
    } catch (IOException e) {
        throw new ARException("Couldn't convert the http response to an inputstream.",e);
    }
    return responseObject;
}

1 个答案:

答案 0 :(得分:2)

因为你必须在单独的线程中使用网络而不是主要的。您可以使用AsyncTaskThread + Handler。如果您使用的是AsyncTask,则必须在网络中使用doInBackground部分。