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 6222781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:22:26+00:00 2026-05-24T08:22:26+00:00

I am trying to write a snippet of code that takes a URL and

  • 0

I am trying to write a snippet of code that takes a URL and displays its textual contents to an EditText view. This is not going well, I have milled around other links that I thought gave the answer such as making my network calls from an AsyncTask described here:

Android Honeycomb: Fragment not able to start AsyncTask?

but that doesn’t seem to work. It is really one function (that calls another) that is all I am trying to use here. Those functions are posted for completeness:

  public static InputStream getInputStreamFromUrl(String url){
            InputStream contentStream = null;

            try{
              HttpClient httpclient = new DefaultHttpClient();
              HttpResponse response = httpclient.execute(new HttpGet(url));
              contentStream = response.getEntity().getContent();
            } catch(Exception e){
               e.printStackTrace();
            }
            return contentStream;
         }

  public static String getStringFromUrl(String url)  {
         BufferedReader br = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));

         StringBuffer sb = new StringBuffer();

         try{
          String line = null;

          while ((line = br.readLine())!=null){
           sb.append(line);
          }
         }catch (IOException e){
          e.printStackTrace();
         }
         return sb.toString();
  }

and these are called from my:

private class FragmentHttpHelper extends AsyncTask<Void, Void, Boolean>{
         protected void onPostExecute(Boolean result) {
                    contractTextTxt.setText(getStringFromUrl(urlReferenceTxt.getText().toString()));
            }
        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return true;
        }         
    }

Which is executed when the button to fetch url is clicked:

        retrieveURLReferenceBtn.setOnClickListener(new OnClickListener() {  
        public void onClick(View arg0) {
                new FragmentHttpHelper().execute();
        }
    });

So by putting things in an asynctask I thought I was going to get around the honeycomb 3.0
NetworkOnMainThreadException but it seems not. Any ideas what to try next?

  • 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-24T08:22:28+00:00Added an answer on May 24, 2026 at 8:22 am

    It looks like you aren’t putting your actual network calls in the doInBackground method in AsyncTask. That is generally what you would want to do, and THEN onPostExecute will be called. I think your onPostExecute is getting called as soon as you start the AsyncTask, because you don’t do anything in doInBackground.

    Try moving your HttpClient code into the doInBackground method.

    From what I can tell its running like this:

    Button clicked > Calls AsyncTask > Nothing Happens in Background thread > calls postExecute which tries to .setText(NETWORK CALL) … which is back on your UI thread.

    Correct me if I am wrong. Maybe I am seeing this wrong?

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

Sidebar

Related Questions

I am trying to write a jQuery snippet of code, that will parse an
I'm trying to write a vs code snippet that will take the selected and
I m trying write code that after reset set up rrpmax as 3000. It
Trying to write a code at the moment that basically tests to see if
Possible Duplicate: CURL alternative in Python I'm trying to write a python snippet that
I found this snippet of code here that allows you to log into a
I am trying to write XS glue code for a serialization/deserialization library that is
I am trying write a function that generates simulated data but if the simulated
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file

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.