所以,我現在有一個問題,前不久關於GSON我被告知要使用一些所謂的AsyncTaskGSON Android和異步任務
儘管關於如何使用這個discribtion是非常短暫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;
}
}
現在這工作正常exepect即時得到一個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到我的項目:
任何人都可以告訴我,爲什麼即時通訊類沒有找到執行?
你在你的\ libs文件夾中有gson jar文件嗎? –
@EmilAdz我有他們根據我的參考庫 –