gpt4 book ai didi

android - 在 retrofit 2 和 gson 中无法获得特殊字符

转载 作者:太空狗 更新时间:2023-10-29 14:48:33 25 4
gpt4 key购买 nike

我正在尝试从 Web 服务获取 json 列表。这是服务器返回的 json 字符串:[{"categoryName":"Política"},{"categoryName":"Economía"},{"categoryName":"Cultura"},{"categoryName":"Deportes"}问题是转换为 POJO。特殊字符 (í) 看起来像“Politica”。这是改造调用函数:

  @GET("categories")
public Call<List<CategoryPojo>> getCategorias(@Query("sitename") String site)

这是回调函数:

    Call<List<CategoryPojo>> call = restservice.getApiService().getCategorias(medio);

try {
call.enqueue(new Callback<List<CategoryPojo>>() {
@Override
public void onResponse(Call<List<CategoryPojo>> call, Response<List<CategoryPojo>> response) {
List<CategoryPojo> categories = response.body();
if (listener != null)
listener.onDataLoaded(categories);
}

@Override
public void onFailure(Call<List<CategoryPojo>> call, Throwable throwable) {
Log.e("Retrofit Error", throwable.getMessage());

}
});

这是 POJO:

public class CategoryPojo implements Serializable{

public CategoryPojo() { }

@SerializedName("categoryName")
private String name;

public String getName()
{
return this.name;
}


}

请求 Web 服务的结果(在浏览器中输出)是:

[{"categoryName":"Política"},{"categoryName":"Economía"},{"categoryName":"Cultura"},{"categoryName":"Deportes"},{"categoryName":"Salud"},{"categoryName":"Ciencia y Tecnología"},{"categoryName":"Medio Ambiente"},{"categoryName":"Medios"},{"categoryName":"Militar e Inteligencia"},{"categoryName":"Sociedad"}]

所以,返回的 json 有一个很好的编码...我认为这可能与改造读取响应的方式有关。我正在使用 retrofit-2.0.2、gson-2.6.1、converter-gson-2.0.2、okhttp-3.2.0。有什么帮助吗?请

最佳答案

您应该检查响应 header 中的Content-type。查找 charset 值并尝试在后端将其更改为 application/josn;charset=UTF-8。那对我有用。

关于android - 在 retrofit 2 和 gson 中无法获得特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37351233/

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