我以前通过简单地添加 int 或字符串以标准形式使用 List 数组,但我现在正在使用 List 方法查看集合。
我目前正在查看的代码实现了以下类:
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)