Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GSON parsing with Retrofit parsing Flickr JSON response

I seem to have the following problem. I'm using retrofit to get a JSON file and parse it into java object.

The URL i'm considering is: https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2ef592bfddc86f508550184ec706a2fc&tags=gent&per_page=2&format=json

which replies with :

 jsonFlickrApi({"photos":{"page":1,"pages":130008,"perpage":2,"total":"260016","photo":[{"id":"15817874286","owner":"36687364@N07","secret":"e480e6fe0c","server":"7475","farm":8,"title":"3D visualisatie","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15817785516","owner":"36687364@N07","secret":"132f12dff1","server":"7496","farm":8,"title":"schetsontwerp","ispublic":1,"isfriend":0,"isfamily":0}]},"stat":"ok"})

I believe the problem lies in the jsonFlickrApi in front of the response.

When executing the following code:

    @GET("/rest/")
void getPhotos(@Query("method") String method,@Query("api_key") String apiKey,@Query("tags") String tags,@Query("per_page")String perPage,@Query("format") String format, Callback<FlickrResult> data);

and

flickerRestAPI.getPhotos("flickr.photos.search","2ef592bfddc86f508550184ec706a2fc","gent","10","json",new Callback<FlickrResult>() {
        @Override
        public void success(FlickrResult photos, Response response) {
            Log.i(TAG, " " + photos.toString());
        }

        @Override
        public void failure(RetrofitError error) {
            Log.e(TAG,"Failed");
            Log.e(TAG," " +error.getUrl());
            Log.e(TAG," "+error.getMessage());

        }
    });
}

I get

 11-21 19:15:54.476  12058-12058/be.hogent.festivalproject E/MainActivity﹕ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

The wrapper classes I'm using are:

public class FlickrResult {

@SerializedName("photos")
public FlickrPhotos photos;

}

public class FlickrPhotos {

public int page;
public String pages;
public int perpage;
public String total;
public ArrayList<FlickrPhoto> photo;
public String stat;}

public class FlickrPhoto {


public String id;
public String owner;
public String secret;
public String server;
public int farm;
public String title;
public int ispublic;
public int isfriend;
public int isfamily;}
like image 221
Jens Buysse Avatar asked Nov 21 '14 18:11

Jens Buysse


2 Answers

Simply add the nojsoncallback param

https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2ef592bfddc86f508550184ec706a2fc&tags=gent&per_page=2&format=json&nojsoncallback=1

You can find it documented here

https://www.flickr.com/services/api/response.json.html

like image 151
Walialu Avatar answered Nov 13 '22 15:11

Walialu


I had to add &format=json param as well as the nojsoncallback param. Thus a sample request would be:

@GET("?method=flickr.photos.getRecent&format=json&nojsoncallback=1")
like image 35
galaxigirl Avatar answered Nov 13 '22 16:11

galaxigirl