HTT presponse.getEntity()NetworkOnMainThreadException [英] HttpResponse.getEntity() NetworkOnMainThreadException
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/5150637/networkonmainthreadexception">NetworkOnMainThreadException
在很长一段时间,我一直在使用通用的code,做HTTP请求中的AsyncTask
。该的AsyncTask
返回的Htt presponse
对象。一切伟大的工作和GUI线程永远不会冻结或任何东西。
现在,突然之间,这将创建一个 NetworkOnMainThreadException
:
serverResponse.getEntity()的getContent();
什么鬼?为什么 getEntity()
考虑网络?在我看来,该行只是转换成一个InputStream的回应,不应该需要一个网络连接。谁做了这个决定?为什么他们决定这应该是网络?
异步任务:
公共类AsyncHttpTask扩展的AsyncTask&LT; Htt的prequestInfo,整型的Htt prequestInfo&GT; {
公共AsyncHttpTask(){
超();
}
受保护的Htt prequestInfo doInBackground(Htt的prequestInfo ... PARAMS){
的Htt prequestInfo rinfo = PARAMS [0];
尝试{
HttpClient的客户端=新DefaultHttpClient();
HTT presponse RESP = client.execute(rinfo.getRequest());
rinfo.setResponse(RESP);
}
赶上(例外五){
rinfo.setException(E);
}
返回rinfo;
}
@覆盖
保护无效onPostExecute(Htt的prequestInfo rinfo){
super.onPostExecute(rinfo);
rinfo.requestFinished();
}
回调接口:
公共接口HttpCallback {
公共无效onResponse(Htt的presponse serverResponse);
公共无效onerror的(例外五);
}
Htt的prequestInfo:
公共类的Htt prequestInfo {
私人HttpUriRequest REQUEST_;
私人HttpCallback callback_;
私人异常EXCEPTION_;
私人的Htt presponse response_;
公众的Htt prequestInfo(HttpUriRequest要求,HttpCallback回调){
超();
REQUEST_ =请求;
callback_ =回调;
}
公共HttpUriRequest调用getRequest(){
返回REQUEST_;
}
公共无效了setRequest(HttpUriRequest要求){
REQUEST_ =请求;
}
公共HttpCallback getCallback(){
返回callback_;
}
公共无效setCallback(HttpCallback回调){
callback_ =回调;
}
公共异常getException(){
返回EXCEPTION_;
}
公共无效setException(例外的例外){
EXCEPTION_ =例外;
}
公众的Htt presponse GETRESPONSE(){
返回response_;
}
公共无效setResponse(Htt的presponse响应){
response_ =响应;
}
公共无效requestFinished(){
如果(EXCEPTION_!= NULL){
callback_.onError(EXCEPTION_);
}
其他 {
callback_.onResponse(response_);
}
}
}
然后我用杰克逊转换的JSON响应的对象。这是这是发生异常:
@覆盖
公众&LT; T&GT;牛逼用handleResponse(Htt的presponse serverResponse,类&LT; T&GT; typeOfResponse){
ObjectMapper映射器=新ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,假);
牛逼responseObject = NULL;
尝试 {
responseObject = mapper.readValue(serverResponse.getEntity()的getContent(),typeOfResponse。); //此行是EVIL
}赶上(JsonParseException E){
抛出新ARException(无法处理响应,因为HTTP响应包含JSON格式不正确,E);
}赶上(JsonMappingException E){
抛出新ARException(映射JSON响应响应对象+ typeOfResponse +失败,E);
}赶上(IllegalStateException异常E){
抛出新ARException(无法转换的HTTP响应,因为非法状态的一个InputStream,E);
}赶上(IOException异常E){
抛出新ARException(无法转换成HTTP响应的InputStream,E);
}
返回responseObject;
}
由于您必须使用网络中的单独的线程,而不是主要的。您可以使用 AsyncTask的或的发 +的处理程序。如果您使用的是AsyncTask的所有工作,网络必须在doInBackground部分执行。
Possible Duplicate:
NetworkOnMainThreadException
For a long time, I've been using generic code that does http requests in an AsyncTask
. The AsyncTask
returns an HttpResponse
object. Everything worked great and the GUI thread never froze or anything.
Now, suddenly, this creates a NetworkOnMainThreadException
:
serverResponse.getEntity().getContent();
What the heck?? Why is getEntity()
considered networking?? In my mind, that line merely converts a response to an inputstream and should not need a network connection. Who made this decision? WHY did they decide this should be networking?
The async task:
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();
}
Callback interface:
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_);
}
}
}
Then I use jackson to convert the json response to an object. That's this is where the exception occurs:
@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;
}
Because you must work with network in separate thread and not main. You can use AsyncTask or Thread + Handler. If you are using AsyncTask all work with network you must perform in doInBackground part.
这篇关于HTT presponse.getEntity()NetworkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!