Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:16:53+00:00 2026-06-14T14:16:53+00:00

Possible Duplicate: NetworkOnMainThreadException For a long time, I’ve been using generic code that does

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:16:54+00:00Added an answer on June 14, 2026 at 2:16 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Android HttpClient : NetworkOnMainThreadException I am working on an application that is
Possible Duplicate: Strange NetworkOnMainThreadException in Android app? Trying To Upload To Dropbox: NetworkOnMainThreadException? I
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Android HttpClient : NetworkOnMainThreadException Im having troubles to create an android app
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Difference of create Index by using include column or not using Edit:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.