【问题标题】:retrofit error JSONSyntaxException改造错误 JSONSyntaxException
【发布时间】:2015-04-14 12:26:34
【问题描述】:

我最近决定使用 Retrofit Library for android 来进行网络调用,并使用 GSON 来处理 JSON。但是我不断收到以下异常并且我的应用程序崩溃了。

Caused by: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:383)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy0.getSMS(Native Method)
at package$AsyncTask.doInBackground()
Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:67)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
... 10 more
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
... 11 more
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:387)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:189)
... 14 more

我使用邮递员检查 JSON 响应是

{"success":"true",
"message":"We are going to rock you"}

我的界面如下:

public interface CreateHTTPRequest {
public static final String END_POINT1 = "http://baseurl.com";

@POST("/user/usermob")
public NewRes getSMS(@Query(value = "mobile") String mobile);

// This is used as Model for return type
public static class NewRes {
    String success;
    String message;
}

我在 AsyncTask 中发出网络请求如下:

        RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint(CreateHTTPRequest.END_POINT1 /*"https://api.github.com"*/)
        .build();

        CreateHTTPRequest request = restAdapter.create(CreateHTTPRequest.class);
        NewRes response = request.getSMS("123456789");

我不明白可能出了什么问题。我是使用 Retrofit 和 GSON 的新手,所以无法弄清楚一切。

【问题讨论】:

  • 你有什么解决办法吗?
  • 我使用了改造的调试工具。似乎在服务器端没有以预期的格式收到 json。我认为这是在早期版本的改造 1.7-1.8 中提出的。事情可能已经改变,但我记得在改造中启用调试帮助我解决了我的问题。

标签: java android json gson retrofit


【解决方案1】:

尝试对您的 API 调用使用 Retrofit 回调:

public interface CreateHTTPRequest {
    public static final String END_POINT1 = "http://baseurl.com";

    @POST("/user/usermob")
    void getSMS(@Query(value = "mobile") String mobile, Callback<NewRes> callback);
}

取出NewRes并使其成为一个新类:

public class NewRes {
    String success;
    String message;
} 

将此行NewRes response = request.getSMS("123456789"); 更改为:

final NewRes newRes;
request.getSMS("123456789", new Callback<NewRes>() {

    @Override
    public void failure(RetrofitError error) {
        //Do Something on Error
    }

    @Override
    public void success(NewRes newResResponse, Response response) {
        newRes=newResResponse;
    }
});

希望这有帮助,祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2015-12-28
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多