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

北慕城南 提交于 2019-12-08 13:18:49

问题


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!


回答1:


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



回答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

""


来源:https://stackoverflow.com/questions/39591467/using-retrofit-2-but-have-expected-begin-object-but-was-string-at-line-1-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!