我正在尝试使用返回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
的Exception的:
java.lang.NullPointerException: Attempt to invoke virtual method 'long com.mapps.pixabayclient.models.RequestModel.getTotal()' on a null object reference
我尝试调试它,一切对我来说似乎还不错,我不知道自己错过了什么。如果有人注意到错误,我将非常感激。
感谢您的提前帮助。
答案 0 :(得分:0)
问题出在Product
注释中,因为我将api /?键移到GET注释的括号内-> const Page2 = ({content}) => {
const data = useFragment(
graphql`
fragment Page2_content on Page2Info {
allProducts { //returns an array of products
title
...Product_detail
}
}
`,
detail
)
return (
<div>
{data.allProducts.map(product => <button>{product.name}</button>)}
{*/Display the product detail of the product who's button is clicked*/}
<Product detail={data.allProducts}/>
</div>
}
但是现在的问题是我应该在每个GET或任何其他请求类型中传递密钥。
是否有一种变通方法来系统地在每个请求中传递密钥?
答案 1 :(得分:0)
因此,您已经发现的问题是,通过设置,密钥已从请求中删除。这是我通过您的设置发出请求时的响应。
Response {protocol = http / 1.1,code = 400,message = Bad Request,url = https://pixabay.com/api/}
但事实并非如此。根据杰克·沃顿(Jake Wharton)的this,如果我们使用@GET(".")
,则整个base_url都将成为请求URL。出于某种原因,对您而言并非如此。 [也许,如果我们使用一些拦截器记录请求的URL,那么我们可以更好地了解发生了什么(无论是改造问题还是后端问题)。]
无论如何,一种解决方案是为每个请求传递api密钥,如下所示:
@GET(".")
public Call<RequestModel> getRequest(@Query("key") String key);
并调用如下函数:
api.getRequest("my_api_key")
另一种解决方案是像您已经将api密钥硬编码到@GET("api/?key=xxxxx")
中。
更好的解决方案是use okhttp interceptor拦截请求,并在url中添加api键查询参数。使用此解决方案,您无需为每个函数调用传递api密钥。