[英]GSON parsing with Retrofit parsing Flickr JSON response
I seem to have the following problem. 我好像有以下问题。 I'm using retrofit to get a JSON file and parse it into java object.
我正在使用改造来获取JSON文件并将其解析为java对象。
The URL i'm considering is: https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2ef592bfddc86f508550184ec706a2fc&tags=gent&per_page=2&format=json 我正在考虑的网址是: https : //api.flickr.com/services/rest/?method = tvl.photos.search&api_key = 2ef592bfddc86f508550184ec706a2fc&targs =gent&per_page = 2&format = json
which replies with : 回复:
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"})
I believe the problem lies in the jsonFlickrApi in front of the response. 我相信问题在于响应前面的jsonFlickrApi。
When executing the following code: 执行以下代码时:
@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);
and 和
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());
}
});
}
I get 我明白了
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 $
The wrapper classes I'm using are: 我正在使用的包装类是:
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;}
Simply add the nojsoncallback param 只需添加nojsoncallback参数即可
https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2ef592bfddc86f508550184ec706a2fc&tags=gent&per_page=2&format=json&nojsoncallback=1 https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=2ef592bfddc86f508550184ec706a2fc&tags=gent&per_page=2&format=json&nojsoncallback=1
You can find it documented here 你可以在这里找到它
https://www.flickr.com/services/api/response.json.html https://www.flickr.com/services/api/response.json.html
I had to add &format=json
param as well as the nojsoncallback param. 我不得不添加
&format=json
param以及nojsoncallback param。 Thus a sample request would be: 因此,样本请求将是:
@GET("?method=flickr.photos.getRecent&format=json&nojsoncallback=1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.