使用Android Studio反序列化GSON时出现错误
作者:互联网
我正在开发一个在我的应用程序中使用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$1.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$1.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库的示例
标签:gson,android-studio,serialization,deserialization,android 来源: https://codeday.me/bug/20191119/2035959.html