[英]Android Retrofit I don't receive JSON
我是Android開發的新手,我正嘗試在Android Studio項目中使用Retrofit和Gson來從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復制並粘貼到Web瀏覽器中,則可以看到很多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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.