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

  • SEARCH
  • Home
  • 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 7507927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:35:01+00:00 2026-05-29T22:35:01+00:00

Now I’m solving the android.os.NetworkOnMainThreadException I put the connect method into separated thread, but

  • 0

Now I’m solving the

android.os.NetworkOnMainThreadException

I put the connect method into separated thread, but when the thread starts, the start() method doesn’t invoke the Run(),
– and also using the AsyncTask, the task doesn’t invoke the doInBackground() method!


// For The AsyncTask my code was

public class ConnectTask extends AsyncTask<URL, Integer, HttpEntity> {

    HttpEntity entity;
    String  statue;
    LogInJSONActivity mainActivity;
    @Override
    protected HttpEntity doInBackground(URL... arg0) {
        // TODO Auto-generated method stub
         mainActivity.setString("Inside Do in background");
         entity = connect(arg0[0]);
         mainActivity.setHttp(entity);
        return entity;
    }

    public  HttpEntity connect(String url) {

        statue= "Inside Connect";
        HttpClient httpclient = new DefaultHttpClient();
        // Prepare a request object
        HttpGet httpget = new HttpGet(url);
        // Execute the request
        HttpResponse response;
        HttpEntity entity = null;
        try {
            response = httpclient.execute(httpget);
            entity = response.getEntity();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return entity;
    }
    public void setActivity(LogInJSONActivity act){
        mainActivity = act;
    }
}

-> In The Main Activity

URL url = null;
        try {
            url = new URL("http://www.flickr.com/photos/51469488@N03/");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ConnectTask backgroundTask = (ConnectTask) new ConnectTask();
        backgroundTask.setActivity(this);
        backgroundTask.execute(url);

// For The Thread My code Was :-

public class ConnectThread extends Thread {

    HttpEntity entity;
    String  statue;
    LogInJSONActivity mainActivity;
    @Override
    public void run() {
        // TODO Auto-generated method stub
         mainActivity.setString("Inside Run");
         entity = connect("http://www.flickr.com/photos/51469488@N03/");
         mainActivity.setHttp(entity);
         super.run();   
    }

    public  HttpEntity connect(String url) {

        statue= "Inside Connect";
        HttpClient httpclient = new DefaultHttpClient();
        // Prepare a request object
        HttpGet httpget = new HttpGet(url);
        // Execute the request
        HttpResponse response;
        HttpEntity entity = null;
        try {
            response = httpclient.execute(httpget);
            entity = response.getEntity();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(entity !=null)
            System.out.print("Valid Entity");
        else
            System.out.print("Null Entity");


        return entity;
    }
    public void setActivity(LogInJSONActivity act){
        mainActivity = act;
    }      
}

-> In the MainActivity

ConnectThread t = new ConnectThread();
        t.setActivity(this);
        t.start();
  • 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-05-29T22:35:02+00:00Added an answer on May 29, 2026 at 10:35 pm

    Your error is probably because you are doing some Network Operation on your UI thread, which is not allowed from API 11.I would say not to create a seperate Thread, just use AsyncTask class to execute your Network operation. Just do your Network Operation in doInBackground() and call the AsyncTask class from the UI thread as,

    new YourAsyncTask().execute(url);
    

    Thats all no need to put much efforts by creating a seperate Thread class and making it more complicated.

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

Sidebar

Related Questions

Now this might look like a duplicate thread, but my question is that I
Now that I know C++ I want to get into desktop application that have
now i need to insert some data from the sqlserver into a word,i know
Now this is .NET but I am sure the principal should apply to all
Now this might sound simple, but I'm a bit mixed up. I am trying
Now that my facebook app has got more than 1000 likes -- https://apps.facebook.com/156485447732146/ But
this is what i have right now Drawing an RSS feed into the php,
Now that Rails 3 escapes all evaluated elements in views (good), but it is
Now this might be a dumb question, but I'm still new to using Visual
Now that I'm working in F#, is it time to start writing up some

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.