nnnn
nnnn

Reputation: 63

Using Retrofit 2 but, have Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

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!

Upvotes: 0

Views: 8938

Answers (2)

Robert Estivill
Robert Estivill

Reputation: 12477

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": [
     ...
   ]
  }
 })

Upvotes: 3

Rudresh
Rudresh

Reputation: 751

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

""

Upvotes: 2

Related Questions