Reputation: 73
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:
char[] buffer = new char[16384];
for (cnt=0; cnt<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
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:
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
What I'm trying to read is Google's weather API, by the way. It looks like this:
<?xml version="1.0"?><xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
...
</weather>
</xml_api_reply>
Adding some debugging code indicates the exception is triggered by this line:
inStream = new BufferedInputStream(urlConnection.getInputStream());
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?
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!
Upvotes: 0
Views: 522
Reputation: 39228
The problem does not appear to have anything to do with your URL-downloading code. The stack trace is leading you to a call to Float#valueOf(String) on line 270 of ShowInfoQuif.java
:
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)
The String
argument that is being passed to Float#valueOf(String) is not in a recognized floating point number format.
See if you can trace where the invalid String
value is coming from. It might help to log the string using Log#d(String, String) before calling Float#valueOf(String).
Upvotes: 1