java - Retrofit (Android) - Getting An Object Returned From Callback -


tried searching question, couldn't find (possibly due inexperience java/retrofit).

i instance of imgurapi class , call imgurapi::getimage():

imgurapi imgurapi = imgurapi.getinstance(); image image = imgurapi.getimage(someidhere); 

i make request image, , 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 requestedimage = image return         }          @override         public void failure(retrofiterror error) {             system.out.println("failed: " + error.tostring());         }     });     return requestedimage; } 

the issue having determining proper way image returned in success() activity called imgurapi::getimage().

originally had global variable in imgurapi image image, , assign retrieved image, , theni return that: return imgurapi.image;. worked, felt incorrect.

does have recommendation properly? or going wrong way?

edit solution

i able more searching after getting couple answers, , found solve issue:

imgurapi imgurapi = imgurapi.getinstance(); imgurapi.getimage("oheryma", new callback<image>() {     @override     public void success(image image, response response) {         // simple test check 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() contains:

public void getimage(final string id, callback<image> callback) {     imgurservice.getimage(id, callback); } 

that not possible. imgurservice.getimage(...) method asynchronous, , can not (should not) synchronously wait finish. instead, use callback<image> execute code wanted execute when method return image.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -