gpt4 book ai didi

java - Android Retrofit 我没有收到 JSON

转载 作者:行者123 更新时间:2023-12-01 10:52:31 27 4
gpt4 key购买 nike

我是 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);

}

然后我在我的 fragment 中使用它,如下所示:

    @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,但主体为空。但是,如果我在调试 session 中展开响应并将 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。结果返回一个带有“照片”字段的对象。你需要一个包装类 -

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;
}

关于java - Android Retrofit 我没有收到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com