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/45703156/

相关文章:

android - 为什么调用 Process.killProcess(Process.myPid()) 是个坏主意?

c# 将带有对象列表的 json 字符串转换为 c# 对象

php - jsonp -> json_decode()

java命令行执行

Java 从 int var 获取第一个和最后一个 2 字节

java - 遍历 pig 中的数组

java - 二维数组作为类变量

Android如何避免当用户杀死相关应用程序时AccessibilityService被杀死

php - 使用 PHP 解析 JSON

android - 漏洞? Fragment 中未显示以编程方式添加的内容