使用Retrofit将JSON对象数组转换为List

时间:2016-12-30 16:22:04

标签: java android list arraylist retrofit2

问候和Mrry Xmas,我有这个JSON对象数组如下:

[{"AgencyName":"Head-Office/Branch","AgencyLocation":"Immeuble Grand Carrefour Rue Marie Gocker,Yaounde","AgencyPhoneNumber":"+237222229610/691698762","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Yaounde-Marche-Centrale Branch","AgencyLocation":"Immeuble Grand Carrefour Rue Marie Gocker,Yaounde","AgencyPhoneNumber":"+237222041661/22229604/91697426","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Bamenda Branch","AgencyLocation":"Evidence Building, City Chemist Roundabout,Bamenda","AgencyPhoneNumber":"+237222041665/233364170/691697553","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Deido Branch","AgencyLocation":"Face Boulangerie COAF, Douala","AgencyPhoneNumber":"+237222041660/633402641/691697494","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Bafut Branch","AgencyLocation":"Midland Centre, 3 Corners Njinteh, Bafut","AgencyPhoneNumber":"+237675025263/691698716","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Buea Branch","AgencyLocation":"University Junction, Molyko, Buea","AgencyPhoneNumber":" +237222041664/333323322/691698625","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Bonaberi Branch","AgencyLocation":"Cimetière, Immeuble Pharmacie Bonaberi, Douala","AgencyPhoneNumber":"+237222041663/333392710/691697617","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Biyem-Assi Branch","AgencyLocation":"Carrefour Biyem-assi ,Yaounde","AgencyPhoneNumber":"+237222041662/222316710/691698667","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Limbe Branch","AgencyLocation":"Down Beach, Sappa Road, Limbe","AgencyPhoneNumber":"+237222041690/222041693/691698628","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Kribi Branch","AgencyLocation":"Adjacent Auto Ecole Française Rue Petit Paris, Kribi","AgencyPhoneNumber":"+237222041691/222041692/691698632","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Akwa Branch","AgencyLocation":"Boulevard de la Liberté, Douala","AgencyPhoneNumber":"+237222041670/691698663","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Dakar Branch","AgencyLocation":"Marché Bilongué, Douala","AgencyPhoneNumber":"+237691698627","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Bambili Branch","AgencyLocation":"Three Corners, Bambili","AgencyPhoneNumber":"+237222054199/691697441","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Tchinga Branch","AgencyLocation":"Ave du 27 Août 1940,Yaounde","AgencyPhoneNumber":"691907381","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"},{"AgencyName":"Kumba Branch","AgencyLocation":"Kumba","AgencyPhoneNumber":"+237222041664","AgencyGPSCoordinates":"GPS Position","AgencyHours":"8:00-16:00"}]

这里是相应的类(使用jsonschema2pojo):

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class AgencyModel {

    @SerializedName("AgencyName")
    @Expose
    private String agencyName;
    @SerializedName("AgencyLocation")
    @Expose
    private String agencyLocation;
    @SerializedName("AgencyPhoneNumber")
    @Expose
    private String agencyPhoneNumber;
    @SerializedName("AgencyGPSCoordinates")
    @Expose
    private String agencyGPSCoordinates;
    @SerializedName("AgencyHours")
    @Expose
    private String agencyHours;

    public String getAgencyName() {
        return agencyName;
    }

    public void setAgencyName(String agencyName) {
        this.agencyName = agencyName;
    }

    public String getAgencyLocation() {
        return agencyLocation;
    }

    public void setAgencyLocation(String agencyLocation) {
        this.agencyLocation = agencyLocation;
    }

    public String getAgencyPhoneNumber() {
        return agencyPhoneNumber;
    }

    public void setAgencyPhoneNumber(String agencyPhoneNumber) {
        this.agencyPhoneNumber = agencyPhoneNumber;
    }

    public String getAgencyGPSCoordinates() {
        return agencyGPSCoordinates;
    }

    public void setAgencyGPSCoordinates(String agencyGPSCoordinates) {
        this.agencyGPSCoordinates = agencyGPSCoordinates;
    }

    public String getAgencyHours() {
        return agencyHours;
    }

    public void setAgencyHours(String agencyHours) {
        this.agencyHours = agencyHours;
    }

}

我正在使用retrofit2来获取对象列表,我看了abit @文档和示例但仍然有错误,这是我的Api类:

    public static UnicsAgencyApi getUnicsAgencyApi() {

            if (sUnicsAgencyApi == null) {
                retrofit = new Retrofit.Builder().baseUrl(ENDPOINT_URL).addConverterFactory(GsonConverterFactory.create())
                        .build();
                sUnicsAgencyApi = retrofit.create(UnicsAgencyApi.class);
            }
            return sUnicsAgencyApi;
        }

        public interface UnicsAgencyApi {

            @GET("api/uconnectservice/AllAgency")
            void getStreams(Callback<List<AgencyModel>> callback);
        }

and here is where i make the request:

    RestApi.getUnicsAgencyApi().getStreams(new Callback <List<AgencyModel>>(){

                @Override
                public void onFailure(Call<List<AgencyModel>> arg0, Throwable arg1) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onResponse(Call<List<AgencyModel>> AgencyModelData, Response<List<AgencyModel>> response) {
                    // TODO Auto-generated method stub
                    //ConsumeApiData(response);
                    **mstreamData.addAll(response);**
                }

            });

错误发生在@ mstreamData.addAll(响应); 我不知道我做错了什么,请任何一个有任何建议或更好的做这件事我非常感谢            欢呼声

1 个答案:

答案 0 :(得分:0)

使用response.body()。在你的代码中你有

mstreamData.addAll(response.body());