gpt4 book ai didi

java - Retrift API 中的 JSON 格式错误?

转载 作者:行者123 更新时间:2023-12-01 08:52:17 25 4
gpt4 key购买 nike

我是 Android 开发新手。这是我的 MainActivity.java。我已经设置了宽松,但仍然收到错误 D/onFailure: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to Accept malformed JSON at line 1 column 1 path $

API 返回:成功或验证,如错误:名字无效。

private void saveUser() {
showpDialog();

Gson gson = new GsonBuilder().setLenient().create();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("BASE_URL")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

APIService service = retrofit.create(APIService.class);
User user = new User();

user.setFirstName(editName.getText().toString());

Call<User> call = service.saveUser(user.getFirstName());

call.enqueue(new Callback<User>() {
@Override
public void onResponse(Response<User> response) {
hidepDialog();
Log.d("onResponse", "" + response.code() +
" response body " + response.body() +
" responseError " + response.errorBody() + " responseMessage " +
response.message());
}

@Override
public void onFailure(Throwable t) {
hidepDialog();
Log.d("onFailure", t.toString());
}
});

}

我一直在寻找,他们都说我需要将 Lenient 设置为 true,但我已经这样做了。有什么想法吗?

最佳答案

SuccessValidation 都不是有效的 JSON。我认为这就是它畸形的原因。 JSON 是这样的:

{ "status": "success" }

因此,您不应将其解析为 JSON,而应将其作为原始字符串读取。或者,最好使来自服务器的响应成为如上所述的有效 JSON。

更好地使其成为 RESTful,即从 HTTP 响应状态而不是响应正文中读取状态,无论是 200 OK 还是其他状态代码。但这是不可能的,所以现在只使用 JSON。

关于java - Retrift API 中的 JSON 格式错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311498/

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