[英]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.