猿问

我想从使用 Retrofit2 获得的 json 数组中获取特定对象的列表。我该怎么做呢?

Json 结构 PS 引用不是动态的,所以只有一个 JSON 数据数组;

我在文档上做了所有事情,但我只收到一个数组,我无法从中获取单独元素的列表,例如该数组中的坐标

主要活动类:

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);






    Service.getInstance()

            .getJSONApi()

            .loadList()

            .enqueue(new Callback<List<Pandomats>>() {



                @Override

                public void onResponse(Call<List<Pandomats>> call, Response<List<Pandomats>> response) {

                    pandomats.addAll(response.body());




                    Log.v("ListPandomats", String.valueOf(pandomats.size()));

                }


                @Override

                public void onFailure(Call<List<Pandomats>> call, Throwable t) {


                }

            });

Service.java :


public class Service {

private static final Service ourInstance = new Service();

private static final String BASE_URL = "http://myurl";

private Retrofit mRetrofit;


public static Service getInstance() {

    return ourInstance;

}


private Service() {

    mRetrofit = new Retrofit.Builder()

            .baseUrl(BASE_URL)

            .addConverterFactory(GsonConverterFactory.create())

            .build();


}


public JsonPlaceApi getJSONApi() {

    return mRetrofit.create(JsonPlaceApi.class);

}


}

JsonPlaceApi.java :


public interface JsonPlaceApi {

@GET("/api/device/get/")

Call<List<Pandomats>> loadList();

}

我需要用 getModel 填充我的列表,例如如何实现它或哪里有错误?



月关宝盒
浏览 97回答 1
1回答

慕少森

通过查看您在问题中发布的示例 JSON 数据格式,我认为 API 不会返回 JSONArray,而是返回JSONObject。不管怎样,我将告诉您如何从已解析的对象(无论是 JSONArray 还是 JSONObject)中获取所需的数据。您几乎接近您正在寻找的解决方案。只需将下面的代码粘贴到onResponse()方法中即可。@Overridepublic void onResponse(Call<List<Pandomats>> call, Response<List<Pandomats>> response) {&nbsp; &nbsp; &nbsp;pandomats.addAll(response.body());&nbsp; &nbsp; &nbsp;Log.v("ListPandomats", String.valueOf(pandomats.size()));&nbsp; &nbsp; &nbsp;for (int i = 0; i < pandomats.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Pandomats p = pandomats.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.v("ListPandomats", p.getModel());&nbsp; &nbsp;// prints model&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.v("ListPandomats", String.valueOf(p.getLatitude()));&nbsp; &nbsp;// prints latitude&nbsp; &nbsp; &nbsp;}}像上面一样,您可以从Pandomats类中获取任何对象。确保pandomats在声明时或在onResponse()方法内部使用它之前已初始化 ArrayList。否则你最终会得到NullPointerException.并且不要忘记在onFailure()方法内部记录来自 API 的错误响应。这很重要。@Overridepublic void onFailure(Call<List<Pandomats>> call, Throwable t) {&nbsp; &nbsp; Log.e("ListPandomats", "Error" t);}正如我之前所说,我认为 API 不会返回 JSONArray,而是重新运行 JSONObject。如果它返回 JSONObject,则需要像下面这样更改代码。JSONApi.javapublic interface JsonPlaceApi {&nbsp; &nbsp; @GET("/api/device/get/")&nbsp; &nbsp; Call<Pandomats> loadList();&nbsp; // remove List<> from return type&nbsp;}MainActivity.javaService.getInstance()&nbsp; &nbsp; .getJSONApi()&nbsp; &nbsp; .loadList()&nbsp; &nbsp; .enqueue(new Callback<Pandomats>() {&nbsp; /* remove List<> */&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(Call<Pandomats> call, /* remove List<> */ Response<List<Pandomats>> response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Pandomats p = response.body();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // without for loop iteration you can get the data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.v("ListPandomats", p.getModel());&nbsp; &nbsp;// prints model&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.v("ListPandomats", String.valueOf(p.getLatitude()));&nbsp; &nbsp;// prints latitude&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(Call<Pandomats> call, Throwable t) { /* remove List<> */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("ListPandomats", "Error" t);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });我希望现在一切都清楚了。如果您遇到错误,请先查看错误日志。如果不明白,请编辑问题并发布错误日志。
随时随地看视频慕课网APP

相关分类

Java
我要回答