Android 改造我没有收到 JSON
Android Retrofit I don't receive JSON
我是 Android 开发的新手,我正在尝试在 Android Studio 项目中使用 Retrofit 和 Gson 从 Flickr API 中提取数据。但我收到 200 响应,但正文是空的。但是,如果我使用调试器并复制响应的 URL 并将其粘贴到我的浏览器中,我可以看到很多 JSON.
我有这些模型 类,它应该是 java 等同于 API 中的 JSON:
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;
}
}
我已经声明了这样的 Retrofit 服务:
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。如果您有任何想法,请提供帮助。卡在这上面好久了!
这也是我的依赖项在模块中的样子: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 还差一级。在具有 "photos" 字段的对象中,结果是 return。你需要一个包装器 class -
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;
}
我是 Android 开发的新手,我正在尝试在 Android Studio 项目中使用 Retrofit 和 Gson 从 Flickr API 中提取数据。但我收到 200 响应,但正文是空的。但是,如果我使用调试器并复制响应的 URL 并将其粘贴到我的浏览器中,我可以看到很多 JSON.
我有这些模型 类,它应该是 java 等同于 API 中的 JSON:
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;
}
}
我已经声明了这样的 Retrofit 服务:
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。如果您有任何想法,请提供帮助。卡在这上面好久了!
这也是我的依赖项在模块中的样子: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 还差一级。在具有 "photos" 字段的对象中,结果是 return。你需要一个包装器 class -
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;
}