Heya,我得到一个非常奇怪的错误,我试图在所有Android平台上从1.5到2.2(在手机和模拟器上)将对象序列化为JSON时无法理清.
我收到此错误:
E/AndroidRuntime(21017): Uncaught handler: thread AsyncTask #2 exiting due to uncaught exception
E/AndroidRuntime(21017): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(21017): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(21017): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(21017): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(21017): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(21017): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(21017): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
E/AndroidRuntime(21017): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
E/AndroidRuntime(21017): at java.lang.Thread.run(Thread.java:1096)
E/AndroidRuntime(21017): Caused by: java.lang.NoSuchMethodError: org.json.JSONStringer.object
E/AndroidRuntime(21017): at com.qype.radar.api.json.JsonSerializationHelper.startModel(JsonSerializationHelper.java:119)
E/AndroidRuntime(21017): at com.qype.radar.api.json.JsonSerializationHelper.serializeCheckin(JsonSerializationHelper.java:94)
E/AndroidRuntime(21017): at com.qype.radar.api.QypeApiImpl.submitCheckin(QypeApiImpl.java:157)
E/AndroidRuntime(21017): at com.qype.radar.activities.tasks.SubmitCheckinTask.doCheckedInBackground(SubmitCheckinTask.java:29)
E/AndroidRuntime(21017): at com.qype.radar.activities.tasks.SubmitCheckinTask.doCheckedInBackground(SubmitCheckinTask.java:1)
E/AndroidRuntime(21017): at com.github.droidfu.concurrent.BetterAsyncTask.doInBackground(BetterAsyncTask.java:154)
E/AndroidRuntime(21017): at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(21017): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
E/AndroidRuntime(21017): ... 4 more
当在此代码中序列化评论时从’startModel()’到达’stringer.object’行时:
package com.qype.radar.api.json;
import org.json.JSONException;
import org.json.JSONStringer;
import com.qype.radar.model.Review;
public class JsonSerializationHelper {
private JSONStringer stringer = new JSONStringer();
...
public String serializeReview(Review review) throws JSONException {
startModel("review");
value("rating",review.getRating());
value("language",review.getLanguage());
value("content",review.getText());
return endModel();
}
...
private JSONStringer startModel(String name) throws JSONException {
stringer.object();
stringer.key(name);
stringer.object();
return stringer;
}
private String endModel() throws JSONException {
stringer.endObject();
stringer.endObject();
return stringer.toString();
}
private void value(String key,String value) throws JSONException {
stringer.key(key);
stringer.value(value);
}
private void value(String key,int value) throws JSONException {
stringer.key(key);
stringer.value(value);
}
}
关于这一点最奇怪的是代码编译很好,并且在调试时,JSONStringer对象被实例化为ok,但是对它的任何调用都会抛出此错误.我也能够使用Java的反射发现该类确实存在,并且我可以从类对象中检索的方法都存在,并且可以无错误地调用.
org.json包是Android中的标准,自API版本1起可用.
任何帮助解决这个问题将不胜感激,因为我根本不知道在哪里看.
最佳答案
原文链接:https://www.f2er.com/android/430301.html