com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGI

报错如下:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGI_第1张图片


2020-03-05 18:41:55.161 5576-5576/com.xiayiye.takeout W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 3 column 14 path $.data
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:225)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at java.lang.Thread.run(Thread.java:761)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err: Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 3 column 14 path $.data
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at com.google.gson.stream.JsonReader.nextString(JsonReader.java:825)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:402)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:390)
2020-03-05 18:41:55.162 5576-5576/com.xiayiye.takeout W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
2020-03-05 18:41:55.163 5576-5576/com.xiayiye.takeout W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
2020-03-05 18:41:55.163 5576-5576/com.xiayiye.takeout W/System.err: 	... 9 more

说白了错误原因是因为服务器给你返回的是object类型,但是你用的String接收的导致错误了

解决方法一:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGI_第2张图片com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGI_第3张图片

改完后拿到数据了:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGI_第4张图片

解决方法二:

就是让后台将整个data对象放到“”双引号中以String的类型返给你

 

你可能感兴趣的:(Android开发错误记录)