title: HttpResponse using android issue: execute always causes exception?
body: <p>I've been working on an Android project and I'm at a point where I want to ask some API for information.  Seems like this should be very basic!</p>
  
  <p>Here's the general gist of my code:</p>
  
  <pre><code>private InputStream retrieveStream2(String url)
  {
      DefaultHttpClient client = new DefaultHttpClient();
  
      HttpGet getRequest = new HttpGet(url);
      System.out.println("getRequest == " + getRequest);
      try {
  
  
          HttpResponse getResponse = client.execute(getRequest);//here is teh problem
          final int statusCode = getResponse.getStatusLine().getStatusCode();
  
  
          if (statusCode != HttpStatus.SC_OK)
          {
              Log.w(getClass().getSimpleName(),
                      "Error " + statusCode + " for URL " + url);
              return null;
          }
  
          HttpEntity getResponseEntity = getResponse.getEntity();
          return getResponseEntity.getContent();
  
      }
      catch (Exception e)
      {
          getRequest.abort();
          Log.w(getClass().getSimpleName(), "Error for URL, YO " + url, e);
      }
      return null;
  }
  </code></pre>
  
  <p>where url variable is the string "http://search.twitter.com/search.json?q=javacodegeeks".</p>
  
  <p>As you can see there is some nice JSON info at that site;  My problem is that every time ''client.execute(getRequest);'' is called, the program throws and catches an exception.  Not useful!  </p>
  
  <p>I've heard two things:  </p>
  
  <p>1) You have to set permission for the emulator/device to use the internet!
    -- I think I covered this, but maybe I did  it wrong! 
    In the androidmanifest.xml I added </p>
  
  <pre><code>&lt; uses-permission android:name="android.permission.INTERNET" &gt;&lt; /uses-permission&gt;
  </code></pre>
  
  <p>So there's that.</p>
  
  <p>2) (which I'm not so certain about) you cannot start a 'networking' thread within a 'ui' thread.  I'm not entirely sure what this means, but i went ahead and followed some tutorial on Android Threads, Handlers and AsyncTasks.  Here:  Please check out the code under the AsyncTask tutorial, which I followed:</p>
  
  <p><a href="http://www.vogella.de/articles/AndroidPerformance/article.html">http://www.vogella.de/articles/AndroidPerformance/article.html</a></p>
  
  <p>After following along with the AsyncTask tutorial, I found that I still had the same problem--</p>
  
  <p>the line:
  HttpGet httpGet = new HttpGet(url)
  always threw an exception, like before.</p>
  
  <p>Here is the logcat from my attempt with the threading tutorial above:</p>
  
  <pre><code>02-27 20:43:28.565: I/ActivityManager(92): START {cmp=com.Prometheus.R1/.JsonParsingActivity} from pid 574
  02-27 20:43:28.565: W/WindowManager(92): Failure taking screenshot for (180x300) to layer 21010
  02-27 20:43:28.896: I/System.out(574): pre execute
  02-27 20:43:29.236: I/ActivityManager(92): Displayed com.Prometheus.R1/.JsonParsingActivity: +638ms
  02-27 20:43:29.329: I/ARMAssembler(35): generated scanline__00000077:03010104_00008001_00000000 [ 89 ipp] (110 ins) at [0x40fad6a8:0x40fad860] in 7204915 ns
  02-27 20:43:30.016: W/System.err(574): java.net.UnknownHostException: Unable to resolve host "search.twitter.com": No address associated with hostname
  02-27 20:43:30.016: W/System.err(574):  at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
  02-27 20:43:30.026: W/System.err(574):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
  02-27 20:43:30.026: W/System.err(574):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
  02-27 20:43:30.026: W/System.err(574):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
  02-27 20:43:30.036: W/System.err(574):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  02-27 20:43:30.036: W/System.err(574):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  02-27 20:43:30.046: W/System.err(574):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
  02-27 20:43:30.046: W/System.err(574):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  02-27 20:43:30.046: W/System.err(574):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  02-27 20:43:30.055: W/System.err(574):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
  02-27 20:43:30.055: W/System.err(574):  at com.Prometheus.R1.JsonParsingActivity$DownloadWebPageTask.doInBackground(JsonParsingActivity.java:88)
  02-27 20:43:30.055: W/System.err(574):  at com.Prometheus.R1.JsonParsingActivity$DownloadWebPageTask.doInBackground(JsonParsingActivity.java:1)
  02-27 20:43:30.055: W/System.err(574):  at android.os.AsyncTask$2.call(AsyncTask.java:264)&lt;br/&gt;
  02-27 20:43:30.066: W/System.err(574):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  02-27 20:43:30.066: W/System.err(574):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)&lt;br/&gt;
  02-27 20:43:30.066: W/System.err(574):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
  02-27 20:43:30.076: W/System.err(574):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  02-27 20:43:30.076: W/System.err(574):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  02-27 20:43:30.087: W/System.err(574):  at java.lang.Thread.run(Thread.java:856)
  02-27 20:43:30.108: W/System.err(574): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
  02-27 20:43:30.116: W/System.err(574):  at libcore.io.Posix.getaddrinfo(Native Method)
  02-27 20:43:30.116: W/System.err(574):  at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
  02-27 20:43:30.126: W/System.err(574):  at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
  02-27 20:43:30.126: W/System.err(574):  ... 18 more
  02-27 20:43:30.136: I/System.out(574): Except thrown by url http://search.twitter.com/search.json?q=javacodegeeks, .... 
  02-27 20:43:30.136: I/System.out(574): response =
  </code></pre>
  
  <p><br/>
  The exception is an UnknownHostException as you can see: <br/>
  "ava.net.UnknownHostException: Unable to resolve host "search.twitter.com": No address associated with hostname"
  <br/>
  But I don't think the site is unacceptable...</p>
  
  <p>Can anyone tell me what's going on + what I need to do to get through it?</p>