Exception thrown when trying to read text from a URL
<p>I'm a relative newbee to Android/Java and about to despair over a problem I can't seem to fix:  I'm trying to read a few datapoints from an XML API website. Since I need to read it for five different API calls in a row and there is a lot of data on the website (about 1.3kB each), I thought it best to read the whole thing as text and simply parse it for the data I need.  I'm using this code:</p>

<pre><code>      char[] buffer = new char[16384];

      for (cnt=0; cnt&lt;numcities; cnt++)
        {
            try 
              {
              url = new URL(urlStrings[cnt]);
              urlConnection = (HttpURLConnection) url.openConnection();

              try 
                {
                inStream = new BufferedInputStream(urlConnection.getInputStream());

                if (inStream != null)
                  {
                  try 
                     {
                    reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));

                     while ((charcount = reader.read(buffer)) != -1) 
                       {
                       writer.write(buffer, 0, charcount);
                       }
                     } 
                   finally 
                     {
                     inStream.close();
                    inputLine = writer.toString();
                    }
                  }
                } 
              finally 
                {
                urlConnection.disconnect();
                }
              }
        catch (MalformedURLException e) 
          {
          }
        catch (IOException e) 
          {
          e.printStackTrace(); 
          }

      if (inputLine != null)
        {
// process the text and extract the data; the data points from five API calls all end up in one string eventually
   }
} // end for loop
</code></pre>

<p>This code worked fine until last week; suddenly (without me having changed anything), the app crashes, on the emulator and on my phone (the same app that worked last week!) with an exception thrown, as follows: </p>

<pre><code>threadid=7: thread exiting with uncaught exception (group=0x4001d800)
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.NumberFormatException: 
at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)
at java.lang.Float.parseFloat(Float.java:291)
at java.lang.Float.valueOf(Float.java:330)
at com.gbg.android.china.ShowInfoQuif$DownloadROETask.doInBackground(ShowInfoQuif.java:270)
at com.gbg.android.china.ShowInfoQuif$DownloadROETask.doInBackground(ShowInfoQuif.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 4 more
</code></pre>

<p>What I'm trying to read is Google's weather API, by the way. It looks like this:</p>

<pre><code>&lt;?xml version="1.0"?&gt;&lt;xml_api_reply version="1"&gt;
&lt;weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" &gt;
  ...
&lt;/weather&gt;
&lt;/xml_api_reply&gt;
</code></pre>

<p>Adding some debugging code indicates the exception is triggered by this line:</p>

<pre><code>                    inStream = new BufferedInputStream(urlConnection.getInputStream());
</code></pre>

<p>Strangely, the exception does not always happen at the same point. Usually, it happens on cnt=1, when the second URL is called. However, the app sometimes crashes at the first, third, or last one. Another funny thing is the debugging info indicates all five URLs are correctly read and the expected result returned, even though Android then exits the app. What could be wrong here?</p>

<p>I know the fixed buffer size I'm using is not ideal but suspect that's not the issue here. Anybody have a suggestion how to make this work or at least how to catch the exception?  I would surely appreciate it!</p>
