【发布时间】:2020-09-12 14:52:24
【问题描述】:
我正在尝试使用返回 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 转换为对象,我创建了两个类:
第一个是: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 中数组的第二个类是:
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
我尝试调试它,对我来说一切都很好,我不知道我错过了什么。如果有人能注意到这个错误,我将非常感激。
提前感谢您的帮助。
【问题讨论】:
-
首先检查
response.isSuccessful()并记录结果以查看问题所在。同时记录 http 响应代码以了解发生了什么 -
inside on response set
RequestModel req = response.body();然后尝试获取req.getTotal() -
@sonnet,很奇怪,它给出了 400 错误代码,我不知道为什么我更新了帖子并将 GSON 转换器用来查看该级别错误的接口。
-
这可能是有问题的
@GET(".")。可以发一下你的网址吗?不必精确。例如:https://example.com/api/hits什么的 -
@sonnet,好吧,我去。
标签: java android json gson retrofit2