如何使用Retrofit从JsonArray获取单个对象?

时间:2018-04-26 23:37:13

标签: android arrays json retrofit retrofit2

我正在尝试使用Retrofit从我的API获取数组。然后,我需要从数组中获取某些对象,以便在我的应用程序的不同部分中使用。使用下面的代码,我可以获得成功的响应,但是当我尝试记录响应时,我得到了这个......

myapp.com.myapp.LocationsModel@9a0d130

我需要返回实际数组,以便从数组中获取对象(字符串)。我曾尝试过谷歌和Stack Overflow的几种不同的实现方式,但是我不能让它在我的生活中发挥作用。也许我忽略了一些简单的事情。这是我第一次使用Retrofit而不是Volley。谢谢你的帮助。这是我的LocationsModel模型。我还粘贴了我正在尝试接收和解析的api响应。

模型

@GET("locations")
    Call<List<LocationsModel>> getLocations(@Header("authorization") String token);

客户端

public void getContests() {
       // SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String token = LoginActivity.authToken;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Interface.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Interface api = retrofit.create(Interface.class);
            Log.v("sendtoken", token);

        Call<List<LocationsModel>> call = api.getLocations(token);



        call.enqueue(new Callback<List<LocationsModel>>() {
            @Override
            public void onResponse(Call<List<LocationsModel>> call, Response<List<LocationsModel>> response) {
                int statusCode = response.code();
                Log.d("Response Code", "Response Code: " + statusCode);
                Log.d("LocArray", "arr: " + Arrays.toString(response.body().toArray()));

            }

            @Override
            public void onFailure(Call<List<LocationsModel>> call, Throwable t) {
                Log.e("response-failure", call.toString());
                t.printStackTrace();
            }
        });
    }

API Json数组响应

[
    {
        "how_it_works": [
            "Test program"
        ],
        "id": 242,
        "business_id": 162,
        "title": "Take 10% off",
        "desc_short": "10% off",
        "logo_url": "http://www.logourl.com",
        "hashtag": "#Octo",
        "confirm_message_title": "Confirm Message",
        "rules": "
The rules

",
        "confirm_message": "Confirmation",
        "start_date": "2018-04-01T05:00:00.000Z",
        "end_date": "2018-06-01T04:59:59.000Z",
        "active": true,
        "entry_count": 0,
        "created_at": "2018-04-16T19:15:03.000Z",
        "updated_at": "2018-04-16T19:15:57.000Z",
        "deleted_at": null,
        "locations": [
            {
                "id": 232,
                "business_id": 162,
                "name": "Trent Home",
                "address": "123 Second Place",
                "city": "Rochester",
                "state": "OK",
                "zipcode": "35854",
                "latitude": 33.0785,
                "longitude": -87.5819,
                "radius": 280.14,
                "created_at": "2018-04-16T19:15:40.000Z",
                "updated_at": "2018-04-16T19:15:40.000Z",
                "deleted_at": null,
                "contest_locations": {
                    "created_at": "2018-04-16T19:15:48.000Z",
                    "updated_at": "2018-04-16T19:15:48.000Z",
                    "contest_id": 242,
                    "location_id": 232
                },
                "stickers": [
                    "http://www.stickerurl.com"
                ]
            }
        ],
        "entries": []
    },
    {
        "how_it_works": [
            "Take a photo, get a reward"
        ],
        "id": 292,
        "business_id": 162,
        "title": "Take 15% off",
        "desc_short": "15% off $10",
        "logo_url": "https://www.logourl.com",
        "hashtag": "#testAndroid",
        "confirm_message_title": "15% off now",
        "rules": "
rules here

",
        "confirm_message": "Complete good job",
        "start_date": "2018-04-09T05:00:00.000Z",
        "end_date": "2018-05-29T04:59:59.000Z",
        "active": true,
        "entry_count": 0,
        "created_at": "2018-04-23T16:44:42.000Z",
        "updated_at": "2018-04-23T16:44:56.000Z",
        "deleted_at": null,
        "locations": [
            {
                "id": 232,
                "business_id": 162,
                "name": "Trent Home",
                "address": "123 Main St",
                "city": "Tuls",
                "state": "Ny",
                "zipcode": "35235",
                "latitude": 33.0785,
                "longitude": -87.5819,
                "radius": 280.14,
                "created_at": "2018-04-16T19:15:40.000Z",
                "updated_at": "2018-04-16T19:15:40.000Z",
                "deleted_at": null,
                "contest_locations": {
                    "created_at": "2018-04-23T16:44:49.000Z",
                    "updated_at": "2018-04-23T16:44:49.000Z",
                    "contest_id": 292,
                    "location_id": 232
                },
                "stickers": []
            }
        ],
        "entries": []
    }
]

0 个答案:

没有答案