Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Async Http Get request

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 ?

like image 435
Blondy314 Avatar asked Oct 27 '12 14:10

Blondy314


2 Answers

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);
    }
  }
like image 97
Devangi Desai Avatar answered Sep 27 '22 17:09

Devangi Desai


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.

like image 36
Marcin Orlowski Avatar answered Sep 27 '22 18:09

Marcin Orlowski