DemonCode
DemonCode

Reputation: 23

Retrofit (Android) - Getting An Object Returned From Callback

Tried searching for this question, but couldn't find it (possibly due to my inexperience with Java/Retrofit).

I get an instance of the ImgurAPI class and call ImgurAPI::getImage():

ImgurAPI imgurAPI = ImgurAPI.getInstance();
Image image = ImgurAPI.getImage(someIDHere);

I make a request to get an Image, and get a response:

public Image getImage(final String id) {
    Image requestedImage = null;

    imgurService.getImage(id, new Callback<Image>() {
        @Override
        public void success(Image image, Response response) {
            //I need to get requestedImage = image to return
        }

        @Override
        public void failure(RetrofitError error) {
            System.out.println("Failed: " + error.toString());
        }
    });
    return requestedImage;
}

The issue I am having is determining a proper way to get the image returned in success() back to the Activity that called ImgurAPI::getImage().

Originally I had a global variable in ImgurAPI Image image, and I would assign that to the retrieved Image, and thenI would return that: return ImgurAPI.image;. It worked, but it felt incorrect.

Does anyone have a recommendation to do this properly? Or am I going about this the wrong way?

EDIT WITH SOLUTION

I was able to do more searching after getting a couple answers, and found this to solve my issue:

ImgurAPI imgurAPI = ImgurAPI.getInstance();
imgurAPI.getImage("OhERyMa", new Callback<Image>() {
    @Override
    public void success(Image image, Response response) {
        // Simple test to check for functionality
        Context context = getApplicationContext();
        String link = image.getData().getLink();
        LinearLayout ll = (LinearLayout)findViewById(R.id.imageScrollerLayout);
        ImageView iv = new ImageView(context);
        Picasso.with(context).load(link).into(iv);
        ll.addView(iv);
    }

    @Override
    public void failure(RetrofitError error) {
        System.out.println(error.toString());
    }
});

And ImgurAPI::getImage() now contains:

public void getImage(final String id, Callback<Image> callback) {
    imgurService.getImage(id, callback);
}

Upvotes: 2

Views: 2861

Answers (2)

cyroxis
cyroxis

Reputation: 3711

You need to use the callback pattern, if you cant use your results directly from from where getImage is defined you can pass a callback into the method to handle the results.

public interface ImageLoadedCallback { 
    void onImageLoaded(Image image);
}

public void getImage(final String id, final ImageLoadedCallback callback) {
    Image requestedImage = null;

    imgurService.getImage(id, new Callback<Image>() {
        @Override
        public void success(Image image, Response response) {
           callback.onImageLoaded(image);
        }

        @Override
        public void failure(RetrofitError error) {
            System.out.println("Failed: " + error.toString());
        }
    });
}

Upvotes: 0

nhaarman
nhaarman

Reputation: 100448

That is not possible. The ImgurService.getImage(...) method is asynchronous, and you can not (should not) synchronously wait for it to finish. Instead, use the Callback<Image> to execute the code you wanted to execute when your method would return the image.

Upvotes: 2

Related Questions