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!

5
  • 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 ? Commented Sep 20, 2016 at 10:16
  • @Robert Estivill no i have the same respons, as in api.flickr.com/services/rest/… Commented Sep 20, 2016 at 10:20
  • What's your Retrofit interface ? Commented Sep 20, 2016 at 10:22
  • @GET("rest") Call<FlickrResult> getPhotos(@Query("method") String method, @Query("api_key") String key, @Query("format") String format, @Query ("nojsoncallbac") String nojsoncallbac ); Commented Sep 20, 2016 at 10:23
  • Probably unrelated, but FlickrPhotos.pages and FlickrPhotos.total are both integers not Strings Commented Sep 20, 2016 at 10:27

2 Answers 2

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": [
     ...
   ]
  }
 })
Sign up to request clarification or add additional context in comments.

Comments

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

""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.