简体   繁体   中英

Retrofit parsing JSON response null values Android

Hi i cant work out why I am getting null values for my response. Im using Retrofit library on Android.

raw json

{
"images": [
{
  "image": {
    "name": "nike adver",
    "url": "http:\/\/wallpaperbarcelona.com\/wp-content\/uploads\/2013\/07\/neymar-nike-advert.jpg",
    "type": "photo"
  }
}]
}

// interface

public interface PromoImagesAPI {
@GET("/FriendsCMS/images/?type=photo&format=json")
void promoImages(Callback<ImagesObject> callback);

}

request function

private void requestNewsData(String uri) {
RestAdapter api = new RestAdapter.Builder().setEndpoint(ENDPOINT).build();
PromoImagesAPI restapi = api.create(PromoImagesAPI.class);

restapi.promoImages(new Callback<Images>() {

@Override
public void success(Images imageObjects, Response response) {
     System.out.println("webservice " +response.getUrl());

     for (int i = 0; i < imageObjects.images.size(); i++) {
         System.out.println("webservice " +imageObjects.getImages().get(i).getUrl());
         }
      }

 @Override
 public void failure(RetrofitError error) {
     System.out.println("webservice " + error);
        }
    });

} Pojo

public class ImagesObject {
public List<Images> images;

public class Images {

    public String name;
    public String url;
    public String type;


    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }

    public String getType() {
        return type;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setType(String type) {
        this.type = type;
    }
}

}

The thing is the amount of elements in the for loop is correct, i have tested that, the values are all null. Have i missed something, any help would be gratefully appreciated . thanks

use http://www.jsonschema2pojo.org/ to create your java object model and do the following to call

public interface PromoImagesAPI {
@GET("/FriendsCMS/images/?type=photo&format=json")
void promoImages(Callback<Images> callback);

Yes, I answer by agreeing. I also missed the @Expose annotation. This can happen specially when using third party tools to convert from json to kotlin or java classes. I also used Gson to convert from json when doing unit testing and everything was passing gracefully until I ran the app and everything came back with null values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM