0

I make request get photos from Flickr, using Retrofit 2.

I create a class for parsing it:

    public class FlickrResult {

    @SerializedName("photos")
    @Expose
    private FlickrPhotos photos;

    @SerializedName("stat")
    @Expose
    private String stat;

    public FlickrPhotos getPhotos() {
        return photos;
    }


    public class FlickrPhotos {

        @SerializedName("page")
        @Expose
        private int page;

        @SerializedName("pages")
        @Expose
        private String pages;

        @SerializedName("perpage")
        @Expose
        private int perpage;

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

        @SerializedName("photo")
        @Expose
        private ArrayList<FlickrPhoto> photo;

        public ArrayList<FlickrPhoto> getPhoto() {
            return photo;
        }


        public class FlickrPhoto {

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

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

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

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

            @SerializedName("farm")
            @Expose
            private int farm;

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

            @SerializedName("ispublic")
            @Expose
            private int ispublic;

            @SerializedName("isfriend")
            @Expose
            private int isfriend;

            @SerializedName("isfamily")
            @Expose
            private int isfamily;

            public String getTitle() {
                return title;
            }
        }

    }

  }

My build request is:

 static {
        gson = new GsonBuilder()
                .setLenient()
                .create();
    }



 @NonNull
    private static Retrofit buildRetrofit() {
        Log.i(TAG, "onBuildRetrofitApiFactory");
        return new Retrofit.Builder()
                .baseUrl("https://api.flickr.com/services/")
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }

Retrofit interface

@GET("rest")  
        Call<FlickrResult> getPhotos(@Query("method") String method,
                                     @Query("api_key") String key,
                                     @Query("format") String format,
                                     @Query ("nojsoncallbac") String nojsoncallbac
                                                    );

Me responsable is success, but problem in parsing. Have exeption:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

Please guys, i need you help!

nnnn
  • 63
  • 2
  • 8
  • What's the flicker's api response? If the response structure varies depending on the response code then you might have an issue. What's your Retrofit interface ? – Robert Estivill Sep 20 '16 at 10:16
  • @Robert Estivill no i have the same respons, as in https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=eab3aa7d564989fd69048ec505e3e271&format=json&nojsoncallback=1 – nnnn Sep 20 '16 at 10:20
  • What's your Retrofit interface ? – Robert Estivill Sep 20 '16 at 10:22
  • @GET("rest") Call getPhotos(@Query("method") String method, @Query("api_key") String key, @Query("format") String format, @Query ("nojsoncallbac") String nojsoncallbac ); – nnnn Sep 20 '16 at 10:23
  • Probably unrelated, but FlickrPhotos.pages and FlickrPhotos.total are both integers not Strings – Robert Estivill Sep 20 '16 at 10:27

2 Answers2

3

Your Retrofit interface is wrong.

The paremeter "nojsoncallbac" is incorrect and should be "nojsoncallback".

This incorrect parameter causes the API to return a different format on the response

jsonFlickrApi({
  "photos": {
  "page": 1,
  "pages": 10,
  "perpage": 100,
  "total": 1000,
  "photo": [
     ...
   ]
  }
 })
Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
2

Gson is expecting your JSON string to begin with an object opening brace

{

But the string you have passed to it may be starts with an open quotes

""
Rudresh
  • 731
  • 1
  • 10
  • 26