Skip to content Skip to sidebar Skip to footer

How To Return Value Using Async Retrofit 2.0

I am new with retrofit, i have a function with Async Retrofit, with purpose like this example public boolean bookmark(){ boolean result = false; Call call =

Solution 1:

You can use a custom interface. If you pass the interface as parameter to the method "bookmark", you can use it.

try something like:

publicinterfaceBookmarkCallback{
      voidonSuccess(boolean value);
      voidonError();
}

your method should look like:

publicvoidbookmark(final BookmarkCallback callback){
     Call<Response> call = service.bookmark(token, request);
     call.enqueue(newCallback<Response>() {
        @OverridepublicvoidonResponse(Call<Response> call, retrofit2.Response<Response> response) {
             callback.onSuccess(true);
       }

       @OverridepublicvoidonFailure(Call<Response call, Throwable t) {
           callback.onError();
       }
});

When you call this method, you have to pass one callback instance.

Post a Comment for "How To Return Value Using Async Retrofit 2.0"