My Retrofit call.enque() method is getting skipped

2019-03-02 13:26发布

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.

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

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

2条回答
forever°为你锁心
2楼-- · 2019-03-02 13:49

Correct me if I'm wrong, is this on the main thread? That would pose the problem of not waiting for a response.

Consider using async

查看更多
贪生不怕死
3楼-- · 2019-03-02 14:03

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

Please note that you need to call this method on an AsyncTask

EDIT

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

calling the method :

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

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

查看更多
登录 后发表回答