nnnn nnnn - 2 months ago 36
Android Question

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!

Answer

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