这是我的异步任务。在 AsyncTask 中获取异常..
private class GetCategories extends AsyncTask<String, Void, String[]> {
String temp[] = new String[] {};
protected String[] doInBackground(String... urls) {
JSONParser jParser = new JSONParser();
JSONArray category_details;
try {
URL url = new URL(urls[0]);
JSONObject json = jParser.getJSONFromUrl(url.toString());
category_details = json.getJSONArray("cat");
for (int i = 0; i < category_details.length(); i++) {
JSONObject obj = category_details.getJSONObject(i);
temp[i] = obj.getString("catName");
}
return temp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String[] temp) {
}
}
我正在执行任务并获取这样的值。
try {
String[] test = new GetCategories().execute(categories_url).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但我收到这样的错误。我在 doInBackground 方法中遇到异常。
10-11 15:26:47.207: W/System.err(5158): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
10-11 15:26:47.207: W/System.err(5158): at com.example.fragments.CatFragment$GetCategories.doInBackground(CategoriesFragment.java:207)
10-11 15:26:47.207: W/System.err(5158): at com.example.fragments.CatFragment$GetCategories.doInBackground(CategoriesFragment.java:1)
10-11 15:26:47.207: W/System.err(5158): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-11 15:26:47.207: W/System.err(5158): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-11 15:26:47.207: W/System.err(5158): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-11 15:26:47.207: W/System.err(5158): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-11 15:26:47.207: W/System.err(5158): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-11 15:26:47.207: W/System.err(5158): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-11 15:26:47.207: W/System.err(5158): at java.lang.Thread.run(Thread.java:856)
行 207 转到 catch 块。谁能指出我在这里做错了什么?