改造错误JSONSyntaxException

时间:2015-04-14 12:26:35

标签: java android json gson retrofit

我最近决定使用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

我用postman来检查JSON Response

{"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的新手,所以我无法弄清楚eveything。

1 个答案:

答案 0 :(得分:0)

尝试使用Retrofit Callback进行API调用:

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;
    }
});

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