我的Retrofit call.enque()方法被完全跳​​过,不确定为什么 [英] My Retrofit call.enque() method is getting skipped over entirely, not sure why

查看:102
本文介绍了我的Retrofit call.enque()方法被完全跳​​过,不确定为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a call using Retrofit's enqueue() method. I'm calling my refreshImages() in my MainActivity's onCreate(), refreshImages() then calls a method refreshImagesIds() which is supposed to make a call out to Flickr's API and return back a PhotosList object, I'll then pull out the Photos from there which will contain a list of Photo objects. My issue is that for some reason the onResponse() inside my enqueue() method is never getting called. When I use the debugger it skips right over it, and when I put Log statements inside they never get written out. I know the endpoint it is hitting is correct because I can see it using OkHttp's logger, and my POJOs all look to be correct for the data being returned.

有人知道为什么这行不通吗?以下是我的refreshImagesrefreshImagesId.这些都包含在我的MainAcitivty中,并修改了类级变量.

Any idea why this isn't working? Below are my refreshImages and refreshImagesId. These are both contained in my MainAcitivty and modify class-level variables.

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:

And my 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:

Change your call to the synchronous retrofit 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"挂钩,以便知道何时接收到数据,然后用数据通知"客户端:

You could also continue to use enqueue, but you need to provide an "onFinish" hook, so you know when your data has been received and then you "notify" the client with the data:

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

然后您将收到此界面并发送数据:

Then you receive this interface and send data:

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 的库来简化它,我真的向您推荐.

I typically use a library like EventBus to make it easier, I really recommend it to you.

这篇关于我的Retrofit call.enque()方法被完全跳​​过,不确定为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆