1

So i had a question not long ago about GSON now i was told to use something called AsyncTask

Even though the discribtion on how to use this is very brief AsyncTask Doc

I managed to get it to work sort of.

This is my Async class:

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;
}

}

Now this works fine exepect im getting a GSON error:

  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

Please do note that i have added the lib to my project:

enter image description here

Can anyone tell me why im getting the class not found execption?

Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364

1 Answers1

2

The fact that you have it under the Referenced Libraries library is your problem.

what you need to do is to create a \libs in your project folder and place your jar file there.

this will result in placing this library under the Android Dependecies folder in your project (you might need to clear your project/close and reopen Eclipse).

UPDATE:

This screen shot should explain, where you should put your used .jar file for your project.

enter image description here

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • so what your saying is i should create a folder called libs and enter the jars within those – Marc Rasmussen May 15 '13 at 20:49
  • you saved me alot of trouble THANK YOU so much btw is the way im handling the Gson correctly? i mean with the Async – Marc Rasmussen May 15 '13 at 21:04
  • If my answer helped you, you should upvote/accept it. regarding the gson handling, it looks right, take a look at this post: http://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java – Emil Adz May 15 '13 at 21:09