5

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
  • What kind of error are you experiencing? Are you not getting the result? Post a stacktrace if there is any. Commented Oct 27, 2012 at 14:49

2 Answers 2

12

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);
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

1

This is asynchronous operation, therefore anything like:

myRetValue = new myAsyncTask.execute();

will NOT assign return value from onPostExecute() to myRetValue. Your

resultString = returned.toString();

should result in having whatever returned.toString() produced assigned to resultString variable (which I guess is declared outside your AsyncTask class, so basically your code looks fine at first glance. You may simply not having anything in returned. Try planting some breakpoints in your AsyncTask code and inspect if it really behaves as you think it does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.