Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 改型错误JSONSyntaxException_Java_Android_Json_Gson_Retrofit - Fatal编程技术网

Java 改型错误JSONSyntaxException

Java 改型错误JSONSyntaxException,java,android,json,gson,retrofit,Java,Android,Json,Gson,Retrofit,我最近决定使用安卓系统的翻新库进行网络调用,并使用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$R

我最近决定使用安卓系统的翻新库进行网络调用,并使用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响应,它是

{"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;
}
        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");
我在AsyncTask中发出网络请求,如下所示:

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

我不明白会出什么问题。我是新的使用改装和GSON,所以我不能弄清楚一切

尝试对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 response=request.getSMS123456789;致:


希望这有帮助,祝你好运

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