我之前通過簡單地添加int或字符串在標準表單中使用List數組,但我現在正在使用List方法查看集合。是我的LIST <E> AsyncTask理解正確嗎?
林目前正在研究中的代碼實現了以下類:
public class GeoNamesAsyncTask extends AsyncTask<String, Void, List<GeoName>> {
private Context ctx = null;
public GeoNamesAsyncTask(Context ctx) {
this.ctx = ctx;
}
@Override
protected List<GeoName> doInBackground(String... arg0) {
return new GeoNamesService().searchPostalCode(arg0[0]);
}
@Override
protected void onPostExecute(List<GeoName> result) {
this.populateActivity(result);
}
類這種方法是在擴展的AsyncTask超類,並且配備了標準的構造函數設置從調用它的類的上下文。直到現在我明白了。
現在,當'doInBackGround'方法的AsyncTask類的重寫方法變得有點失落時。
據我所知,該代碼正在返回從GeoNameService類的'searchPostalCode'方法運行的(在此代碼的情況下)地名。傳遞的字符串是用戶輸入的郵政編碼。有人能告訴我爲什麼這三個點與字符串一起傳遞?我會認爲它只是'string arg0'而不是'string ... arg0'?
一旦doInBackGround方法返回,我是否認爲'onPostExecute'方法總是直接調用後?
另外我讀過這是不好的做法,用'doInBackGround'方法調用方法。
當我運行這段代碼並嘗試輸入字母和數字的組合(即'E4')時,doInBackGround方法中出現錯誤,但'24501'等整數很好,如圖所示。
02-21 20:05:18.461: E/AndroidRuntime(352): FATAL EXCEPTION: AsyncTask #1
02-21 20:05:18.461: E/AndroidRuntime(352): java.lang.RuntimeException: An error occured while executing doInBackground()
02-21 20:05:18.461: E/AndroidRuntime(352): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.lang.Thread.run(Thread.java:1096)
02-21 20:05:18.461: E/AndroidRuntime(352): Caused by: com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: E4 6AA
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:232)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:222)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.Gson.fromJson(Gson.java:795)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.Gson.fromJson(Gson.java:761)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.Gson.fromJson(Gson.java:710)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.Gson.fromJson(Gson.java:682)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.example.restfulweb.GeoNamesService$DataWrapper.fromJson(GeoNamesService.java:85)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.example.restfulweb.GeoNamesService.searchPostalCode(GeoNamesService.java:41)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.example.restfulweb.GeoNamesAsyncTask.doInBackground(GeoNamesAsyncTask.java:21)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.example.restfulweb.GeoNamesAsyncTask.doInBackground(GeoNamesAsyncTask.java:1)
02-21 20:05:18.461: E/AndroidRuntime(352): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-21 20:05:18.461: E/AndroidRuntime(352): ... 4 more
02-21 20:05:18.461: E/AndroidRuntime(352): Caused by: java.lang.NumberFormatException: E4 6AA
02-21 20:05:18.461: E/AndroidRuntime(352): at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:130)
02-21 20:05:18.461: E/AndroidRuntime(352): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:281)
02-21 20:05:18.461: E/AndroidRuntime(352): at java.lang.Double.parseDouble(Double.java:287)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.stream.JsonReader.nextInt(JsonReader.java:599)
02-21 20:05:18.461: E/AndroidRuntime(352): at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:230)
你可以發佈你的代碼爲GeoName對象嗎?此行指向GSON錯誤,可能與您的註釋或您試圖映射數據的方式有關:02-21 20:05:18.461:E/AndroidRuntime(352):引起來源:com.google.gson.JsonSyntaxException :java.lang.NumberFormatException:E4 6AA – 2013-02-21 21:11:33
另外,是的,你不直接調用'doInBackground()'。你想調用'execute()'方法來處理AsyncTask的所有其他必要的調用。 – 2013-02-21 21:12:55