我是Android / Java的新手,并且对于我似乎无法修复的问题感到绝望:我正在尝试从XML API网站上阅读一些数据点。由于我需要连续读取五个不同的API调用并且网站上有大量数据(每个大约1.3kB),我认为最好将整个内容作为文本阅读并简单地解析数据我需要。我正在使用此代码:
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
此代码在上周工作正常;突然(没有我改变任何东西),应用程序崩溃,在模拟器和我的手机(上周工作的同一个应用程序!)上抛出异常,如下所示:
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
顺便说一下,我想读的是Google的天气API。它看起来像这样:
<?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>
添加一些调试代码表示此行触发了异常:
inStream = new BufferedInputStream(urlConnection.getInputStream());
奇怪的是,异常并不总是发生在同一点上。通常,当调用第二个URL时,它发生在cnt = 1上。但是,应用程序有时会在第一个,第三个或最后一个崩溃。另一个有趣的事情是调试信息表明正确读取了所有五个URL并返回了预期结果,即使Android随后退出应用程序。这可能有什么问题?
我知道我使用的固定缓冲区大小并不理想,但怀疑这不是问题。有人建议如何使这项工作或至少如何捕捉异常?我当然会很感激!
答案 0 :(得分:1)
问题似乎与您的网址下载代码无关。堆栈跟踪引导您在ShowInfoQuif.java
的第270行调用Float#valueOf(String):
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)
传递给Float#valueOf(String)的String
参数不是可识别的浮点数格式。
查看是否可以跟踪无效String
值的来源。在调用Float#valueOf(String)之前,使用Log#d(String, String) 记录字符串可能会有所帮助。