[英]How can I get a specific JSON Array from the response using Retrofit2 and GSON?
[英]How to get whole JSON response from api using Retrofit and GSON in android
我是 android 新手,并使用改造进行 api 调用,该调用返回如下所示的 json,我使用改造进行了 api 调用,但它只给了我父 json 数组。不是嵌套的数组。所以任何人都可以帮助我如何获得整个json作为响应?
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<ProductResponse> call = apiService.getProducts();
call.enqueue(new Callback<ProductResponse>() {
@Override
public void onResponse(Call<ProductResponse> call, Response<ProductResponse> response) {
List<Category> categories = response.body().getResults();
Log.d("cat", "Number of categories received: " + categories.size());
}
@Override
public void onFailure(Call<ProductResponse> call, Throwable t) {
// Log error here since request failed
Log.e("cat", t.toString());
}
});
public interface ApiInterface {
@GET("/json")
Call<ProductResponse> getProducts();
}
public class ApiClient {
public static final String BASE_URL = "https://stark-spire-93433.herokuapp.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
我的JSON:
{
"categories": [
{
"id": 1,
"name": " Casuals",
"products": [
{
"id": 1,
"name": "Nike Sneakers",
"date_added": "2016-12-09T11:16:11.000Z",
"variants": [
{
"id": 1,
"color": "Blue",
"size": 42,
"price": 1000
},
{
"id": 2,
"color": "Red",
"size": 42,
"price": 1000
},
{
"id": 3,
"color": "Blue",
"size": 44,
"price": 1200
},
上面的代码是我所熟悉的,所以任何人都可以帮助我获得整个响应,而不仅仅是父类别。
我想你把所有的模型类都做好了。 然后替换
@GET("/json")
Call<ProductResponse> getProducts();
和
@GET("json")
Call<ProductResponse> getProducts();
如果它不起作用,那么只需发表评论。 我还将提供模型类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.