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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:13:22+00:00 2026-06-13T12:13:22+00:00

I am trying to implement a httpClient class which is an AsyncTask (otherwise I

  • 0

I am trying to implement a httpClient class which is an AsyncTask (otherwise I get an exception due to having a connection in my main thread).
I tried something like this:

private class execHttpAsync extends AsyncTask <String, String, HttpResponse>
    {
        public String resultString;

        @Override
        protected HttpResponse doInBackground(String... params) 
        {
            String url = params[0];

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);           
            request.setHeader("Content-Type", "text/xml");
            HttpResponse response;
            try {
                response = httpClient.execute(request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return response;
        }

        @Override
       protected void onPostExecute(HttpResponse result) 
        {
            StringBuffer returned = new StringBuffer();

            InputStream content = result.getEntity().getContent();
            BufferedReader rd = new BufferedReader(new InputStreamReader(content, "UTF-8"));                
            String line;
            while ((line = rd.readLine()) != null) 
            {
                String endOfLine = "";
                returned.append(line + endOfLine);
            }
            content.close(); 

         String retVal = returned.toString();

         try 
         {
             String header = retVal.substring(0, 1);
             if (!header.equals("<"))
              {
                retVal = retVal.replace(header, "");
              }
         } 
         catch (Exception e) 
         {
            // TODO: handle exception
         }

         resultString = returned.toString();

        }

    }

But I need to get the response in the end. I have tried to instantiate this class and then get the response as a member but that didnt work out.
Any suggestions ?

  • 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-13T12:13:22+00:00Added an answer on June 13, 2026 at 12:13 pm

    try this way

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
          String response = "";
          for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
              HttpResponse execute = client.execute(httpGet);
              InputStream content = execute.getEntity().getContent();
    
              BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
              String s = "";
              while ((s = buffer.readLine()) != null) {
                response += s;
              }
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          return response;
        }
    
        @Override
        protected void onPostExecute(String result) {
          textView.setText(result);
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to implement connection pool with Apache HttpClient (4.1.3) and ThreadSafeClientConnManager
I've been trying to implement connection pooling for my application using Apache HTTPClient (v4.1).
I'm trying to implement a thread on a function that starts an HttpClient because
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
Trying to implement AVAudioplayer and get some metering data of the played music, but
I am trying to get the Apache Commons HttpClient library (version 3.1) to ignore
I am currently trying implement a QThread that contains a socket connection. The socket
So right now, I am trying to implement an asynchronous call which calls my
I am trying implement a most recent widget into my tumblr post. So far,
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor

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.