gpt4 book ai didi

java - 改造错误 JSONSyntaxException

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:57 24 4
gpt4 key购买 nike

我最近决定使用 Android 的 Retrofit Library 来进行网络调用,并使用 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;
}

我在 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 很陌生,所以我无法弄清楚一切。

最佳答案

尝试使用 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;
}
});

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

关于java - 改造错误 JSONSyntaxException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29627306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com