Zac Canoy
Zac Canoy

Reputation: 102

JSON Serializer Android

I am using JSONLib 2.4 for the JSONSerializer() method. Every time I start the activity it force closes. I tried throwing a JSONException, but it gave me an error. What's wrong? This is the call:

JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonTxt);

Here's my LogCat output:

01-14 17:29:24.825: W/dalvikvm(13265): Unable to resolve superclass of Lnet/sf/json/JSONException; (1034) 01-14 17:29:24.825: W/dalvikvm(13265): Link of class 'Lnet/sf/json/JSONException;' failed 01-14 17:29:24.825: W/dalvikvm(13265): VFY: unable to resolve exception class 813 (Lnet/sf/json/JSONException;) 01-14 17:29:24.825: W/dalvikvm(13265): VFY: unable to find exception handler at addr 0x2e 01-14 17:29:24.825: W/dalvikvm(13265): VFY: rejected Lnet/sf/json/JSONSerializer;.toJSON (Ljava/lang/Object;Lnet/sf/json/JsonConfig;)Lnet/sf/json/JSON; 01-14 17:29:24.825: W/dalvikvm(13265): VFY: rejecting opcode 0x0d at 0x002e 01-14 17:29:24.825: W/dalvikvm(13265): VFY: rejected Lnet/sf/json/JSONSerializer;.toJSON (Ljava/lang/Object;Lnet/sf/json/JsonConfig;)Lnet/sf/json/JSON; 01-14 17:29:24.825: W/dalvikvm(13265): Verifier rejected class Lnet/sf/json/JSONSerializer; 01-14 17:29:24.856: W/dalvikvm(13265): threadid=11: thread exiting with uncaught exception (group=0x41b28930) 01-14 17:29:24.872: E/AndroidRuntime(13265): FATAL EXCEPTION: AsyncTask #1 01-14 17:29:24.872: E/AndroidRuntime(13265): java.lang.RuntimeException: An error occured while executing doInBackground() 01-14 17:29:24.872: E/AndroidRuntime(13265): at android.os.AsyncTask$3.done(AsyncTask.java:299) 01-14 17:29:24.872: E/AndroidRuntime(13265): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 01-14 17:29:24.872: E/AndroidRuntime(13265): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 01-14 17:29:24.872: E/AndroidRuntime(13265): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 01-14 17:29:24.872: E/AndroidRuntime(13265): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 01-14 17:29:24.872: E/AndroidRuntime(13265): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 01-14 17:29:24.872: E/AndroidRuntime(13265): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 01-14 17:29:24.872: E/AndroidRuntime(13265): at java.lang.Thread.run(Thread.java:856) 01-14 17:29:24.872: E/AndroidRuntime(13265): Caused by: java.lang.VerifyError: net/sf/json/JSONSerializer 01-14 17:29:24.872: E/AndroidRuntime(13265): at com.aer.illbehonest.VideoPlay$ShowTitlesTask.doInBackground(VideoPlay.java:63) 01-14 17:29:24.872: E/AndroidRuntime(13265): at com.aer.illbehonest.VideoPlay$ShowTitlesTask.doInBackground(VideoPlay.java:1) 01-14 17:29:24.872: E/AndroidRuntime(13265): at android.os.AsyncTask$2.call(AsyncTask.java:287) 01-14 17:29:24.872: E/AndroidRuntime(13265): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 01-14 17:29:24.872: E/AndroidRuntime(13265): ... 4 more

Upvotes: 2

Views: 2145

Answers (1)

digitaljoel
digitaljoel

Reputation: 26574

The answer is in the first line of the logcat.

Unable to resolve superclass of Lnet/sf/json/JSONException

If you look at the javadoc for JSONException it shows the class hierarchy. You can see just above JSONException it says org.apache.commons.lang.exception.NestableRuntimeException which means that NestableRuntimeException is the superclass of JSONException, and therefore you need to have NestableRuntimeException in your project in order for JSONException to work.

In the same way that you added JsonLib to your project, you should also add the dependencies of JsonLib. According to mvnrepository, json-lib 2.4 depends on quite a view libraries, with commons-lang 2.5 being one of them. You can see them as reported by mvnrepository here. You can also see them listed on the json-lib project page here.

For a mobile application that seems like a large number of dependencies and will make your apk bigger. I've not used json-lib, but I might suggest using something more self-contained like gson (download).

Upvotes: 2

Related Questions