所以不久前我有一个关于 GSON 的问题,现在我被告知使用一种叫做 AsyncTask 的东西
尽管关于如何使用它的描述非常简短AsyncTask Doc
我设法让它工作。
这是我的异步课程:
public class JsonCollector extends AsyncTask<URL, integer, String> {
private String finalString;
public JsonCollector(){
}
public String test(){
URL url = null;
try {
url = new URL("http://api.heroesofnewerth.com/player_statistics/ranked/nickname/Hieratic/?token=0CZGH8ZI7UR8J2GN");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader reader = null;
String x = "";
try {
reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
x = line;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if (reader !=null) try{reader.close();} catch (IOException ignore) {
}{}
}
this.finalString = x;
return x;
}
@Override
protected String doInBackground(URL... params) {
JsonElement root = new JsonParser().parse(test());
finalString = root.getAsJsonObject().get("rnk_wins").toString();
return "";
}
public String getFinalString(){
return finalString;
}
}
现在这可以正常工作,除非我收到 GSON 错误:
05-15 19:51:05.091: E/AndroidRuntime(1832): FATAL EXCEPTION: AsyncTask #1
05-15 19:51:05.091: E/AndroidRuntime(1832): java.lang.RuntimeException: An error occured while executing doInBackground()
05-15 19:51:05.091: E/AndroidRuntime(1832): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-15 19:51:05.091: E/AndroidRuntime(1832): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-15 19:51:05.091: E/AndroidRuntime(1832): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-15 19:51:05.091: E/AndroidRuntime(1832): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-15 19:51:05.091: E/AndroidRuntime(1832): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-15 19:51:05.091: E/AndroidRuntime(1832): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-15 19:51:05.091: E/AndroidRuntime(1832): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-15 19:51:05.091: E/AndroidRuntime(1832): at java.lang.Thread.run(Thread.java:856)
05-15 19:51:05.091: E/AndroidRuntime(1832): Caused by: java.lang.NoClassDefFoundError: com.google.gson.JsonParser
05-15 19:51:05.091: E/AndroidRuntime(1832): at logic.JsonCollector.doInBackground(JsonCollector.java:47)
05-15 19:51:05.091: E/AndroidRuntime(1832): at logic.JsonCollector.doInBackground(JsonCollector.java:1)
05-15 19:51:05.091: E/AndroidRuntime(1832): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-15 19:51:05.091: E/AndroidRuntime(1832): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-15 19:51:05.091: E/AndroidRuntime(1832): ... 4 more
请注意,我已将 lib 添加到我的项目中:
谁能告诉我为什么我找不到类的执行?