gpt4 book ai didi

android - 关于 Retrofit 和 Gson 的 MalformedJsonException

转载 作者:行者123 更新时间:2023-11-29 02:39:55 26 4
gpt4 key购买 nike

我正在尝试进行改造调用,但遇到了 MalformedJsonException。

这是我的电话:

String idFoto = "1ead0a1f-bbc4-46bd-901c-7988c0e6c68b";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Global.URL_BASE)
.addConverterFactory(GsonConverterFactory.create())
.build();
FotosAPI service = retrofit.create(FotosAPI.class);
Call<String> obtenerFotoCall = service.getFoto(Global.getToken(), idFoto);

这是我的界面:

public interface FotosAPI {
@GET(Global.URL_FOTO + "{id}")
Call<String> getFoto(@Header("Authorization") String token, @Path("id") String id);
}

调用enqueue进入onFailure方法,报错“com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 2 path $”。

我做了这样的更改以设置宽松:

Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Global.URL_BASE)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

然后,我有一个不同的错误:“com.google.gson.stream.MalformedJsonException: Expected value at line 1 column 1 path $”

我认为错误可能出在idFoto字符串值的Gson转换上,但我不知道哪里出了问题。

有人可以帮帮我吗?

谢谢大家!

最佳答案

请检查您从服务器获取的 JSON,在我的例子中,我从服务器获取的 JSON 无效。

尝试从 Postman 或其他工具检查您的 JSON。

关于android - 关于 Retrofit 和 Gson 的 MalformedJsonException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44941347/

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