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);
}
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
Change your call to the synchronous retrofit API :
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:
Then you receive this interface and send data:
calling the method :
I typically use a library like EventBus to make it easier, I really recommend it to you.