我正在尝试使用翻新从api获取数据,但我不知道该怎么做

时间:2019-06-01 07:17:46

标签: java android json retrofit2

{
    "count": 3,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "name": "Shared and Private Rooms in 3 BHK Apartment in Patel Nagar, Delhi",
            "address": {
                "id": 1,
                "street_address": "Swarn Jayanti Park, Sector 10",
                "city": "Patel Nagar",
                "state": "Delhi",
                "pincode": "110085",
                "country": "India",
                "complete_address": "Swarn Jayanti Park, Sector 10, Patel Nagar, Delhi-110085",
                "latitude": 28.6554,
                "longitude": 77.1646
            },
            "description": "",
            "furnish_type": "Fully furnished",
            "house_size": "1400 sq ft",
            "cover_pic_url": null,
            "rent_from": 20000,
            "security_deposit_from": 40000,
            "available_flat_count": 0,
            "available_room_count": 1,
            "available_bed_count": 2,
            "accomodation_allowed_str": "Girls and Boys",
            "house_type": "Apartment",
            "available_from": null,
            "favorited": false
        }

    ]
}

2 个答案:

答案 0 :(得分:0)

您首先需要创建用于存储传入数据的模型。使用http://www.jsonschema2pojo.org创建模型类(pojo)。在您的项目中,创建一个类来构建Retrofit实例。


public class RetrofitBuilder {

    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;
    }
}

为端点创建接口

@GET("{endpoints}")
Call<Response> getResponse();

现在在您的活动或演示者中,创建一个实例以进行api调用。 并得到回应。看看here以获得更多帮助。

答案 1 :(得分:0)

您需要创建一个对象,以采用以下格式从Retrofit Web服务获取响应。

public class ResponseObject {

private int count;
private String next;
private String previous;
private List<ResultsObject> results;

//  Generate getters & Setters below
             .


}

并按照上面getClient的回答,调用Amit Parameshwar方法以访问Retrofits网站服务并从服务器获取数据。