[英]Retrofit response body gives null
我正在尝试使用返回 JSON 的 API,并且我正在使用 Retrofit 来获取响应并解析它
API 重新运行 JSON,如下所示:
{
"total": 1244881,
"totalHits": 500,
"hits": [
{
"id": 5205518,
"pageURL": "https://pixabay.com/photos/landscape-sea-sky-clouds-sunset-5205518/",
"type": "photo",
"tags": "landscape, sea, sky",
"previewURL": "https://cdn.pixabay.com/photo/2020/05/22/14/04/landscape-5205518_150.jpg",
"webformatWidth": 640,
"webformatHeight": 427,
...
...
},
...
...
]
}
对于 GSON 将此 json 转换为 Object 我创建了两个类:第一个是: RquestModel.java
public class RequestModel {
@SerializedName("total")
private long total;
@SerializedName("totalHits")
private long totalHits;
@SerializedName("hits")
private List<Image> hits;
//Getters and setters down here...
}
json 中阵列的第二个 class 是:
public class Image {
@SerializedName("id")
private long id;
@SerializedName("pageURL")
private String pageURL;
@SerializedName("type")
private String type;
@SerializedName("tags")
private String tags;
@SerializedName("previewURL")
private String previewURL;
@SerializedName("previewWidth")
private long previewWidth;
@SerializedName("previewHeight")
private long previewHeight;
@SerializedName("webformatURL")
private String webformatURL;
@SerializedName("webformatWidth")
private long webformatWidth;
@SerializedName("webformatHeight")
private long webformatHeight;
@SerializedName("largeImageURL")
private String largeImageURL;
@SerializedName("imageWidth")
private long imageWidth;
@SerializedName("imageHeight")
private long imageHeight;
@SerializedName("imageSize")
private long imageSize;
@SerializedName("views")
private long views;
@SerializedName("downloads")
private long downloads;
@SerializedName("favorites")
private long favorites;
@SerializedName("likes")
private long likes;
@SerializedName("comments")
private long comments;
@SerializedName("user_id")
private long userId;
@SerializedName("user")
private String user;
@SerializedName("userImageURL")
private String userImageURL;
//Getters and Setters down here .....
}
而对于GSON转换器使用的接口是这样的:
public interface ImageAPIService {
@GET(".")
public Call<RequestModel> getRequest();
}
当我尝试像这样使用回调的响应时:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ImageAPIService api = retrofit.create(ImageAPIService.class);
Call<RequestModel> request = api.getRequest();
request.enqueue(new Callback<RequestModel>() {
@Override
public void onResponse(Call<RequestModel> call, Response<RequestModel> response) {
mViewModel.setTxt(response.body().getTotal() + "");
}
@Override
public void onFailure(Call<RequestModel> call, Throwable t) {
}
});
给出类型的异常: java.lang.NullPointerException
:
java.lang.NullPointerException: Attempt to invoke virtual method 'long com.mapps.pixabayclient.models.RequestModel.getTotal()' on a null object reference
我试图调试它,对我来说一切都很好,我不知道我错过了什么。 如果有人能注意到这个错误,我将非常感激。
并提前感谢您的帮助。
因此,您已经发现的问题是,通过您的设置,密钥已从请求中删除。 这是我使用您的设置提出请求时的响应。
响应{protocol=http/1.1, code=400, message=Bad Request, url= https://pixabay.com/api/ }
但事实并非如此。 根据 Jake Wharton的说法,如果我们使用@GET(".")
,那么整个 base_url 就变成了请求 url。 出于某种原因,您的情况并非如此。 [也许如果我们使用一些拦截器记录请求 url 那么我们可以更好地了解发生了什么(无论是 retrofit 问题还是后端问题)]。
无论如何,一种解决方案是为每个请求传递 api 密钥,如下所示:
@GET(".")
public Call<RequestModel> getRequest(@Query("key") String key);
并像这样调用 function:
api.getRequest("my_api_key")
另一种解决方案是像您已经做的那样将 api 密钥硬编码到 @GET @GET("api/?key=xxxxx")
中。
更好的解决方案是使用okhttp拦截器拦截请求,并在url中添加api键查询参数。 使用此解决方案,您不必为每个 function 调用传递 api 密钥。
问题出在@GET
注释中,因为我将 api/?key 移到了 GET 注释的括号内 -> @GET("api/?key=xxxxxxx")
但现在的问题是我应该将密钥传入每个 GET 或任何其他请求类型。
是否有一种解决方法可以系统地在每个请求中传递密钥?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.