我似乎遇到了以下问题。我使用Retrofit获取JSON文件并将其解析为Java对象。
我要考虑的URL是: https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2ef592bfddc86f508550184ec706a2fc&tags=gent&per_page=2&format=json 该URL返回以下内容:
我使用的包装类是:
我要考虑的URL是: https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2ef592bfddc86f508550184ec706a2fc&tags=gent&per_page=2&format=json 该URL返回以下内容:
jsonFlickrApi({"photos":{"page":1,"pages":130008,"perpage":2,"total":"260016","photo":[{"id":"15817874286","owner":"36687364@N07","secret":"e480e6fe0c","server":"7475","farm":8,"title":"3D visualisatie","ispublic":1,"isfriend":0,"isfamily":0},{"id":"15817785516","owner":"36687364@N07","secret":"132f12dff1","server":"7496","farm":8,"title":"schetsontwerp","ispublic":1,"isfriend":0,"isfamily":0}]},"stat":"ok"})
我认为问题在响应前面的jsonFlickrApi中。
当执行以下代码时:
@GET("/rest/")
void getPhotos(@Query("method") String method,@Query("api_key") String apiKey,@Query("tags") String tags,@Query("per_page")String perPage,@Query("format") String format, Callback<FlickrResult> data);
并且
flickerRestAPI.getPhotos("flickr.photos.search","2ef592bfddc86f508550184ec706a2fc","gent","10","json",new Callback<FlickrResult>() {
@Override
public void success(FlickrResult photos, Response response) {
Log.i(TAG, " " + photos.toString());
}
@Override
public void failure(RetrofitError error) {
Log.e(TAG,"Failed");
Log.e(TAG," " +error.getUrl());
Log.e(TAG," "+error.getMessage());
}
});
}
我得到
11-21 19:15:54.476 12058-12058/be.hogent.festivalproject E/MainActivity﹕ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
我使用的包装类是:
包装类是指:
public class FlickrResult {
@SerializedName("photos")
public FlickrPhotos photos;
}
public class FlickrPhotos {
public int page;
public String pages;
public int perpage;
public String total;
public ArrayList<FlickrPhoto> photo;
public String stat;}
public class FlickrPhoto {
public String id;
public String owner;
public String secret;
public String server;
public int farm;
public String title;
public int ispublic;
public int isfriend;
public int isfamily;}