我正在开发一个在我的应用程序中使用android电话联系人的应用程序,因此首先获取联系人详细信息并将其存储到ArrayList中.之后,我使用gson库序列化了该arrayList,当我反序列化它时遇到错误.
我已经使用gson-2.1.jar来序列化和反序列化联系方式.我收到以下错误日志.
AndroIDRuntime﹕ FATAL EXCEPTION: main com.Google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 8 at com.Google.gson.Gson.fromJson(Gson.java:766) at com.Google.gson.Gson.fromJson(Gson.java:721) at com.Google.gson.Gson.fromJson(Gson.java:670) at com.Google.gson.Gson.fromJson(Gson.java:642) at com.braintechnix.autoloveapp.util.MyGson.deserializer(MyGson.java:28) at com.braintechnix.autoloveapp.fragment.ContactListFragment.onStart(ContactListFragment.java:69) at androID.support.v4.app.Fragment.performStart(Fragment.java:1986) at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1061) at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1207) at androID.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at androID.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572) at androID.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:493) at androID.os.Handler.handleCallback(Handler.java:739) at androID.os.Handler.dispatchMessage(Handler.java:95) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:696) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 8 at com.Google.gson.stream.JsonReader.expect(JsonReader.java:339) at com.Google.gson.stream.JsonReader.beginArray(JsonReader.java:306) at com.Google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70) at com.Google.gson.Gson.fromJson(Gson.java:755) at com.Google.gson.Gson.fromJson(Gson.java:721) at com.Google.gson.Gson.fromJson(Gson.java:670) at com.Google.gson.Gson.fromJson(Gson.java:642) at com.braintechnix.autoloveapp.util.MyGson.deserializer(MyGson.java:28) at com.braintechnix.autoloveapp.fragment.ContactListFragment.onStart(ContactListFragment.java:69) at androID.support.v4.app.Fragment.performStart(Fragment.java:1986) at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1061) at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1207) at androID.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) at androID.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572) at androID.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:493) at androID.os.Handler.handleCallback(Handler.java:739) at androID.os.Handler.dispatchMessage(Handler.java:95) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:696)
以下是我正在使用的代码:
public ArrayList<Contact> deserializer(String JsonString){ //Type collectionType = new Typetoken<Collection<Contact>>(){}.getType(); // Collection<Contact> enums = gson.fromJson(JsonString, collectionType); Contact[] contacts = gson.fromJson(JsonString, Contact[].class); ArrayList<Contact> contactsList = new ArrayList<Contact>(); for(int i = 0; i< contacts.length; i++){ contactsList.add(contacts[i]); } return contactsList; }
解决方法:
没有您得到的Json响应,这真的很难回答,但是从您遇到的错误中,我可以告诉以下代码将为您提供帮助
更新了答案
public ArrayList<Contact> deserializer(String JsonString){ //Type collectionType = new Typetoken<Collection<Contact>>(){}.getType();// Collection<Contact> enums = gson.fromJson(JsonString, collectionType); ArrayList<Contact> contactsList = (new Gson()).fromJson(JsonString,new Typetoken<ArrayList<Contact>>() {}.getType()); return contactsList;}
以下是一些可能有助于您使用Gson库的示例
ExampleOne
ExampleTwo
总结以上是内存溢出为你收集整理的使用Android Studio反序列化GSON时出现错误全部内容,希望文章能够帮你解决使用Android Studio反序列化GSON时出现错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)