Android Retrofit我没有收到JSON
我是Android开发新手,我试图在Android Studio项目中使用Gson的Retrofit来从Flickr API中提取数据。 但我得到了200的回应,但身体是空的。 但是,如果我使用调试器并复制响应的URL并将其粘贴到我的浏览器中,我可以看到很多JSON。
我有这些模型类应该是API的JSON的Java等价物:
public class Photos {
@SerializedName("page")
@Expose
public Integer page;
@SerializedName("pages")
@Expose
public Integer pages;
@SerializedName("perpage")
@Expose
public Integer perpage;
@SerializedName("total")
@Expose
public String total;
@SerializedName("photo")
@Expose
public List<Photo> photo = new ArrayList<Photo>();
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public Integer getPerpage() {
return perpage;
}
public void setPerpage(Integer perpage) {
this.perpage = perpage;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public List<Photo> getPhoto() {
return photo;
}
public void setPhoto(List<Photo> photo) {
this.photo = photo;
}
}
public class Photo {
@SerializedName("id")
@Expose
public String id;
@SerializedName("owner")
@Expose
public String owner;
@SerializedName("secret")
@Expose
public String secret;
@SerializedName("server")
@Expose
public String server;
@SerializedName("farm")
@Expose
public Integer farm;
@SerializedName("title")
@Expose
public String title;
@SerializedName("ispublic")
@Expose
public Integer ispublic;
@SerializedName("isfriend")
@Expose
public Integer isfriend;
@SerializedName("isfamily")
@Expose
public Integer isfamily;
@SerializedName("url_s")
@Expose
public String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Photo() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public Integer getFarm() {
return farm;
}
public void setFarm(Integer farm) {
this.farm = farm;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getIspublic() {
return ispublic;
}
public void setIspublic(Integer ispublic) {
this.ispublic = ispublic;
}
public Integer getIsfriend() {
return isfriend;
}
public void setIsfriend(Integer isfriend) {
this.isfriend = isfriend;
}
public Integer getIsfamily() {
return isfamily;
}
public void setIsfamily(Integer isfamily) {
this.isfamily = isfamily;
}
}
我已经宣布了这样的改进服务:
public interface FlickrService {
@GET("/services/rest")
Call<Photos> getRecentPhotos(@Query("method") String method, @Query("api_key")String apiKey,
@Query("format") String format, @Query("nojsoncallback") String noJson,
@Query("extras") String urls);
}
然后我将它用在我的片段中,如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URl)
.addConverterFactory(GsonConverterFactory.create())
.build();
FlickrService service = retrofit.create(FlickrService.class);
Call<Photos> call = service.getRecentPhotos(METHOD, API_KEY, FORMAT, NOJSONCALLBACK, EXTRAS);
call.enqueue(this);
}
@Override
public void onResponse(Response<Photos> response, Retrofit retrofit) {
mPhotos = response.body().photo; // The response.body is always null even though its a 200 response
}
@Override
public void onFailure(Throwable t) {
Log.i(TAG, t.getMessage());
}
然后,我只是在onResponse里面的一个断点,我收到了一个200,但有一个空的身体。 但是,如果我在调试会话中展开响应并将URL复制粘贴到我的浏览器中,我可以看到很多JSON。 如果你有任何想法,请帮助。 已经停留了很长一段时间!
这也是我的依赖关系在Module:app里面的样子
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:okhttp:2.4.0'
}
您缺少一个JSON级别。 结果是返回带有“照片”字段的对象。 你需要一个包装类 -
class PhotosWrapper {
Photos photos;
// ....getters and setters
}
改变你的界面 -
@GET("/services/rest")
Call<PhotosWrapper> getRecentPhotos(@Query("method") String method, @Query("api_key")String apiKey,
@Query("format") String format, @Query("nojsoncallback") String noJson,
@Query("extras") String urls);
您还需要更改您的呼叫和响应类型 -
Call<PhotosWrapper> call = service.getRecentPhotos(METHOD, API_KEY, FORMAT, NOJSONCALLBACK, EXTRAS);
@Override
public void onResponse(Response<PhotosWrapper> response, Retrofit retrofit) {
// should do some more error checking here (check isSuccess() and null checks
mPhotos = response.body().photos.photo;
}
链接地址: http://www.djcxy.com/p/29507.html
上一篇: Android Retrofit I don't receive JSON
下一篇: Using Retrofit 2.0 to extract information from a network API