title: Using AndroidHttpClient will not work
body: <p>I am trying to use <code>AndroidHttpClient</code> to download a CSV file. For some reason it fails in the line <code>"HttpResponse response httpClient.execute(httpGet, localContext);</code> and simply goes to <code>"finally"</code>.</p>
  
  <p>I've checked the URL in my browser - it is working fine.</p>
  
  <p>I get no information from HttpResponse response - it simply like skips it. </p>
  
  <p>I don't get why. Any ideas?</p>
  
  <p>Thanks
  D           </p>
  
  <pre><code>private ArrayList&lt;String&gt; retrieveStockFinParamsFromYahooApiUri(String yahooApiCall)
      {
          ArrayList&lt;String&gt; stockFinParamsFromYahooApiUriRows = new ArrayList&lt;String&gt;();
          String resultLine = "";
  
          BufferedReader reader = null;
          AndroidHttpClient httpClient = AndroidHttpClient.newInstance("yahooGetStockParams");
          HttpContext localContext = new BasicHttpContext();
          HttpGet httpGet = new HttpGet(yahooApiCall);
  
          try 
          {
              HttpResponse response = httpClient.execute(httpGet, localContext);              
  
              reader = new BufferedReader(new InputStreamReader(
                      response.getEntity().getContent()));
  
              while ((resultLine = reader.readLine()) != null) 
              {
                  stockFinParamsFromYahooApiUriRows.add(resultLine);
              }
          }
          catch (IOException e) 
          {
              e.printStackTrace();
          } 
          finally 
          {
              if (reader != null) 
              {
                  try 
                  {
                      reader.close();
                  } 
                  catch (IOException e) 
                  {
                      e.printStackTrace();
                  }
              }
          }
  
          return stockFinParamsFromYahooApiUriRows;
      }
  </code></pre>
  
  <hr>
  
  <p>FURTHER INVESTIGATION (05.22.2012):</p>
  
  <p>@Snicolas - thank you for your comments. After I read your comments I thought the problem might originate from the fact that I used the emulator and not my actual device to debug this. I thought the emulator might suffer some connection problems. When I tested it on my device - I noticed the problem still occurred - so this was NOT the problem. </p>
  
  <p>So I change my code to <code>catch (Throwable e)</code> as you advised and got the following stack snapshot:</p>
  
  <pre><code>05-22 18:17:41.457: W/System.err(24552): android.os.NetworkOnMainThreadException
  05-22 18:17:41.461: W/System.err(24552):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
  05-22 18:17:41.461: W/System.err(24552):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
  05-22 18:17:41.461: W/System.err(24552):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
  05-22 18:17:41.465: W/System.err(24552):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
  05-22 18:17:41.465: W/System.err(24552):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
  05-22 18:17:41.465: W/System.err(24552):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
  05-22 18:17:41.468: W/System.err(24552):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
  05-22 18:17:41.468: W/System.err(24552):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
  05-22 18:17:41.468: W/System.err(24552):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  05-22 18:17:41.472: W/System.err(24552):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  05-22 18:17:41.472: W/System.err(24552):    at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:257)
  05-22 18:17:41.472: W/System.err(24552):    at com.bewildering.app.StournamentDbAdapter$YahooStocksParams.retrieveStockFinParamsFromYahooApiUri(StournamentDbAdapter.java:1536)
  05-22 18:17:41.476: W/System.err(24552):    at com.bewildering.app.StournamentDbAdapter$YahooStocksParams.run(StournamentDbAdapter.java:1709)
  05-22 18:17:41.476: W/System.err(24552):    at com.bewildering.app.StournamentActivity.onCreate(StournamentActivity.java:65)
  05-22 18:17:41.476: W/System.err(24552):    at android.app.Activity.performCreate(Activity.java:4465)
  05-22 18:17:41.476: W/System.err(24552):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
  05-22 18:17:41.480: W/System.err(24552):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
  05-22 18:17:41.480: W/System.err(24552):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
  05-22 18:17:41.480: W/System.err(24552):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
  05-22 18:17:41.484: W/System.err(24552):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  05-22 18:17:41.484: W/System.err(24552):    at android.os.Handler.dispatchMessage(Handler.java:99)
  05-22 18:17:41.484: W/System.err(24552):    at android.os.Looper.loop(Looper.java:137)
  05-22 18:17:41.484: W/System.err(24552):    at android.app.ActivityThread.main(ActivityThread.java:4424)
  05-22 18:17:41.488: W/System.err(24552):    at java.lang.reflect.Method.invokeNative(Native Method)
  05-22 18:17:41.488: W/System.err(24552):    at java.lang.reflect.Method.invoke(Method.java:511)
  05-22 18:17:41.488: W/System.err(24552):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  05-22 18:17:41.492: W/System.err(24552):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  05-22 18:17:41.492: W/System.err(24552):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>While looking for <code>android.os.NetworkOnMainThreadException</code>I found <a href="http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html" rel="nofollow noreferrer">the documentation</a> which states that this is: <em>The exception that is thrown when an application attempts to perform a networking operation on its main thread.</em> </p>
  
  <p>From that I conclude that I should put my call into an <code>AsyncTask</code>.</p>
  
  <p>Can anyone please confirm that I understand this correctly - or am I missing something here? Strange that I hat to <code>catch (Throwable e)</code> in order to get to the bottom of this.</p>
  
  <hr>
  
  <p>FURTHER INVESTIGATION (05.22.2012):</p>
  
  <p>I can now confirm that this issue comes due to the fact that <em>The exception... is thrown when an application attempts to perform a networking operation on its main thread.</em> As I <a href="https://stackoverflow.com/questions/5150637/networkonmainthreadexception">read</a> this started with honeycomb.</p>
  
  <p>Ths issue now: I get a 301 response from Yahoo (Moved Permanently). This is strange because when I cut paste the URL into the browser it works. </p>
  
  <p>Any idea why this should work in the browser but not in the application? I must mention that this HTTP request receives a CSV file as a response. Could this be an issue?</p>
  
  <hr>
  
  <p>FURTHER INVESTIGATION (05.23.2012):</p>
  
  <p>For some reason when using (in my code) the Yahoo URI <a href="http://finance.yahoo.com/d/quotes.csv?s=" rel="nofollow noreferrer">http://finance.yahoo.com/d/quotes.csv?s=</a> the response is a 301 (Moved Permanently). When using <a href="http://download.finance.yahoo.com/d/quotes.csv?s=" rel="nofollow noreferrer">http://download.finance.yahoo.com/d/quotes.csv?s=</a> instead the response is 200 (OK). Even that both work fine in the browser. I was lucky enough to find <a href="http://help.infinitekind.com/discussions/moneydance-development/14-stock-quote-synchronizer-for-stock-options" rel="nofollow noreferrer">this web page</a> that gave me some clues regarding which URIs Yahoo reacts to. Now I can see the "reader" gets the data and everything is ALMOST fine. I still get some strange exception (still trying to figure it out): after reading all the lines in the response I catch a Throwable (I left it from my previous experiments) and later on in the stack I thus get <code>05-23 08:52:41.258: W/dalvikvm(933): threadid=11: thread exiting with uncaught exception (group=0x40a721f8)</code> and something about an uncaught exception in <code>doInBackground(Void... params)</code>. Still investigating...</p>
  
  <p>Just solved this issue - it was just a mistake in calling the Yahoo API. Had to add <strong>&amp;e=.csv</strong> to the end of the URI for it to work. Just like that: <a href="http://download.finance.yahoo.com/d/quotes.csv?s=KDHIX&amp;f=sl1d1t1c1ohgv&amp;e=.csv" rel="nofollow noreferrer">http://download.finance.yahoo.com/d/quotes.csv?s=KDHIX&amp;f=sl1d1t1c1ohgv&amp;e=.csv</a></p>
  
  <hr>
  
  <p>FINAL CODE</p>
  
  <pre><code>private ArrayList&lt;String&gt; retrieveStockFinParamsFromYahooApiUri(String yahooApiCall)
      {
          ArrayList&lt;String&gt; stockFinParamsFromYahooApiUriRows = new ArrayList&lt;String&gt;();
          String resultLine = "";
  
          BufferedReader reader = null;
          AndroidHttpClient httpClient = AndroidHttpClient.newInstance("yahooGetStockParams");
          HttpContext localContext = new BasicHttpContext();
          HttpGet httpGet = new HttpGet(yahooApiCall);
  
          try 
          {
              HttpResponse response = httpClient.execute(httpGet, localContext);                  
  
              reader = new BufferedReader(new InputStreamReader(
                      response.getEntity().getContent()));
  
              while ((resultLine = reader.readLine()) != null) 
              {
                  stockFinParamsFromYahooApiUriRows.add(resultLine);
              }
  
              if(response != null)
              {
                  try
                  {
                      response.getEntity().consumeContent();
                  }
                  catch (IOException e) 
                  {
                      e.printStackTrace();
                  }
  
              }
          }
          catch (IOException e) 
          {
              e.printStackTrace();                
          } 
          finally 
          {               
              if(httpClient != null)
              {
                  httpClient.close();
              }
  
              if (reader != null) 
              {
                  try 
                  {
                      reader.close();
                  } 
                  catch (IOException e) 
                  {
                      e.printStackTrace();
                  }
              }
          }
  
          return stockFinParamsFromYahooApiUriRows;
      }
  </code></pre>