Android Retrofit I don't receive JSON

I'm new with Android development and I'm trying to use Retrofit with Gson in an Android Studio project to pull data from the Flickr API. But I'm getting a 200 response but the body is empty. However if I use the debugger and copy the URL of the response and paste it in to my browser I can see a lot of JSON.

I have these model classes which should be the java equivalent of the JSON from the API:

public class Photos {
@SerializedName("page")
@Expose
public Integer page;

@SerializedName("pages")
@Expose
public Integer pages;

@SerializedName("perpage")
@Expose
public Integer perpage;

@SerializedName("total")
@Expose
public String total;

@SerializedName("photo")
@Expose
public List<Photo> photo = new ArrayList<Photo>();

public Integer getPage() {
    return page;
}

public void setPage(Integer page) {
    this.page = page;
}

public Integer getPages() {
    return pages;
}

public void setPages(Integer pages) {
    this.pages = pages;
}

public Integer getPerpage() {
    return perpage;
}

public void setPerpage(Integer perpage) {
    this.perpage = perpage;
}

public String getTotal() {
    return total;
}

public void setTotal(String total) {
    this.total = total;
}

public List<Photo> getPhoto() {
    return photo;
}

public void setPhoto(List<Photo> photo) {
    this.photo = photo;
}

}

public class Photo {

@SerializedName("id")
@Expose
public String id;

@SerializedName("owner")
@Expose
public String owner;

@SerializedName("secret")
@Expose
public String secret;

@SerializedName("server")
@Expose
public String server;

@SerializedName("farm")
@Expose
public Integer farm;

@SerializedName("title")
@Expose
public String title;

@SerializedName("ispublic")
@Expose
public Integer ispublic;

@SerializedName("isfriend")
@Expose
public Integer isfriend;

@SerializedName("isfamily")
@Expose
public Integer isfamily;

@SerializedName("url_s")
@Expose
public String url;

public String getUrl() {
    return url;
}

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

public Photo() {
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getOwner() {
    return owner;
}

public void setOwner(String owner) {
    this.owner = owner;
}

public String getSecret() {
    return secret;
}

public void setSecret(String secret) {
    this.secret = secret;
}

public String getServer() {
    return server;
}

public void setServer(String server) {
    this.server = server;
}

public Integer getFarm() {
    return farm;
}

public void setFarm(Integer farm) {
    this.farm = farm;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Integer getIspublic() {
    return ispublic;
}

public void setIspublic(Integer ispublic) {
    this.ispublic = ispublic;
}

public Integer getIsfriend() {
    return isfriend;
}

public void setIsfriend(Integer isfriend) {
    this.isfriend = isfriend;
}

public Integer getIsfamily() {
    return isfamily;
}

public void setIsfamily(Integer isfamily) {
    this.isfamily = isfamily;
}

}

And i have declared a Retrofit service like this:

public interface FlickrService {

@GET("/services/rest")
Call<Photos> getRecentPhotos(@Query("method") String method, @Query("api_key")String apiKey,
                             @Query("format") String format, @Query("nojsoncallback") String noJson,
                             @Query("extras") String urls);

}

and I then use it in my fragment like this:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    FlickrService service = retrofit.create(FlickrService.class);
    Call<Photos> call = service.getRecentPhotos(METHOD, API_KEY, FORMAT, NOJSONCALLBACK, EXTRAS);
    call.enqueue(this);
}

    @Override
public void onResponse(Response<Photos> response, Retrofit retrofit) {
    mPhotos = response.body().photo; // The response.body is always null even though its a 200 response
}

@Override
public void onFailure(Throwable t) {
    Log.i(TAG, t.getMessage());
}

I then but a breakpoint inside onResponse and I receive a 200 but with an empty body. However if I expand the response in the debugging session and copy-paste the URL into my webbrowser I can see alot of JSON. If you have any idea please help. Been stuck at this for quite a while!

Also this is what my dependencies looks like inside Module : app

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.4.0'

}


You are missing one level of your JSON. The result is return in an object with a "photos" field. You need a wrapper class for that -

class PhotosWrapper {
   Photos photos;

   // ....getters and setters
}

change your interface to --

@GET("/services/rest")
Call<PhotosWrapper> getRecentPhotos(@Query("method") String method, @Query("api_key")String apiKey,
                             @Query("format") String format, @Query("nojsoncallback") String noJson,
                             @Query("extras") String urls);

You will also need to change your call and response type --

Call<PhotosWrapper> call = service.getRecentPhotos(METHOD, API_KEY, FORMAT, NOJSONCALLBACK, EXTRAS);

@Override
public void onResponse(Response<PhotosWrapper> response, Retrofit retrofit) {
    // should do some more error checking here (check isSuccess() and null checks
    mPhotos = response.body().photos.photo;
}
链接地址: http://www.djcxy.com/p/29508.html

上一篇: 使用Android App中的Volley库解析对象数据

下一篇: Android Retrofit我没有收到JSON