java - 我的 Retrofit call.enqueue() 方法被完全跳​​过,不知道为什么

标签 java android json rest retrofit

我正在使用 Retrofit 的 enqueue() 方法进行调用。我在 MainActivity 的 onCreate() 中调用 refreshImages()refreshImages() 然后调用一个方法 refreshImagesIds(),该方法应该调用 Flickr 的 API 并返回一个 PhotosList 对象,然后我将从那里提取 Photos,其中将包含 Photo 列表 对象。我的问题是,由于某种原因,我的 enqueue() 方法中的 onResponse() 永远不会被调用。当我使用调试器时,它会直接跳过它,当我将 Log 语句放入其中时,它们永远不会被写出。我知道它所访问的端点是正确的,因为我可以使用 OkHttp 的记录器看到它,并且我的 POJO 对于返回的数据来说看起来都是正确的。

知道为什么这不起作用吗?以下是我的 refreshImagesrefreshImagesId。这些都包含在我的 MainAcitivty 中并修改类级别变量。

private void refreshImages() {
        // make api call
        //imageUrls = FlickrServiceManager_withinterface.getKittenImages(8);

        refreshImageIds();

        List<Photo> photos = photosList.getPhotos().getPhoto();
        imageIds = new ArrayList<String>();
        for(Photo photo : photos) {
            Log.d("TAG", "It is pringint imageIds: " + photo.getId());
            imageIds.add(photo.getId());
        }
}

private void refreshImageIds() {
    Retrofit retrofit = Api.getRestAdapter();
    FlickrServiceInterface flickrService = retrofit.create(FlickrServiceInterface.class);
    Call<PhotosList> call = flickrService.getPhotos(API_KEY, FORMAT, "1");
    imageIds = new ArrayList<String>();

    call.enqueue(new Callback<PhotosList>(){
        @Override
        public void onResponse(Call<PhotosList> call, Response<PhotosList> response) {
            photosList = response.body();
        }

        @Override
        public void onFailure(Call<PhotosList> call, Throwable t) {
            // TODO: Clean up
            Log.d("TEMP_TAG", "Call failed");
        }
    });
}

还有我的 FlickrServiceInterface:

public interface FlickrServiceInterface {

    @GET("?method=flickr.photos.getSizes")
    Call<PhotoSizes> getPhotoSizes(@Query("api_key") String apiKey, @Query("format") String format, @Query("nojsoncallback") String jsonCallback, @Query("photo_id") String photoId);

    @GET("?method=flickr.photos.getRecent")
    Call<PhotosList> getPhotos(@Query("api_key") String apiKey, @Query("format") String format, @Query("nojsoncallback") String jsonCallback);
}

最佳答案

更改对同步改造 API 的调用:

public static List<String> getImageIds(int size) {
    Call<PhotosList> call = flickrService.getPhotos(apiKey, format, "1");
    photoIds = new ArrayList<String>();

    PhotosList photosList = call.execute().body();
    List<Photo> photos = photosList.getPhotos().getPhoto();

    for(Photo photo : photos) {
        Log.d("TEMP_TAG", "adding photo id to list: " + photo.getId());
        photoIds.add(photo.getId());
    }
    Log.d("TEMP_TAG", "it's getting here too");
    return photoIds;
}

请注意,您需要在 AsyncTask 上调用此方法

编辑

您也可以继续使用 enqueue,但您需要提供一个“onFinish” Hook ,以便您知道何时收到数据,然后用数据“通知”客户端:

//interface por communication
public interface ImageIdsCallBack {
   public void onFinish( List<String> photoIds );
}

然后就收到这个接口(interface)并发送数据:

public static List<String> getImageIds(int size, final ImageIdsCallBack callback) {
    Call<PhotosList> call = flickrService.getPhotos(apiKey, format, "1");
    photoIds = new ArrayList<String>();

    call.enqueue(new Callback<PhotosList>(){
        @Override
        public void onResponse(Call<PhotosList> call, Response<PhotosList> response) {
            PhotosList photosList = response.body();
            List<Photo> photos = photosList.getPhotos().getPhoto();

            for(Photo photo : photos) {
                Log.d("TEMP_TAG", "adding photo id to list: " + photo.getId());
                photoIds.add(photo.getId());
            }
            //send the data to the caller
            callback.onFinish(photoIds);
        }

        @Override
        public void onFailure(Call<PhotosList> call, Throwable t) {
            // TODO: Clean up
            Log.d("TEMP_TAG", "Call failed");
        }
    });
    Log.d("TEMP_TAG", "it's getting here too");
    return photoIds;
}

调用方法:

getImageIds( 50 , new ImageIdsCallBack() {
     public void onFinish( List<String> photoIds ) {
         //update UI with photoIds
     }
} );

我通常使用像 EventBus 这样的库为了方便起见,我强烈推荐给您。

关于java - 我的 Retrofit call.enqueue() 方法被完全跳​​过,不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45723664/

相关文章:

json - 使用 GSON 的 JSON Java 对象给出计算器错误

java - 检查系统托盘是否集中在 Java 中?

java - OSGi 服务未实现接口(interface)

java - 如何在带有 i18n 的应用程序中使用 @StringDef

json - 在 Elastic Search 中同步 JSON 数据

javascript - Jsonp 出现 No 'Access-Control-Allow-Origin' 错误

java - Spring MVC Maven Jar hell : The hierarchy of the type "ExtendedMappingJacksonJsonView" is inconsistent

java - 在 MapBox 上添加标记的最佳方法是什么?

android - 使用本地 list 的 repo 不起作用

安卓 : Load image from specific directory