如何使用异步改造2.0返回值 [英] How to return value using Async Retrofit 2.0

查看:198
本文介绍了如何使用异步改造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<Response> call = service.bookmark(token, request);
   call.enqueue(new Callback<Response>() {

      @Override
            public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
            result = true;
      }
      @Override
            public void onFailure(Call<Response call, Throwable t) {

      }
   });

   return result;
}

但我不知道如何来返回值。

but i dont know how to return that value.

推荐答案

您可以使用自定义的界面。如果你传递接口作为参数传递给方法书签,你可以使用它。

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

尝试这样的:

public interface BookmarkCallback{
      void onSuccess(boolean value);
      void onError();
}

你的方法应该是这样的:

your method should look like:

public void bookmark(final BookmarkCallback callback){
     Call<Response> call = service.bookmark(token, request);
     call.enqueue(new Callback<Response>() {
        @Override
        public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
             callback.onSuccess(true);
       }

       @Override
       public void onFailure(Call<Response call, Throwable t) {
           callback.onError();
       }
});

当你调用这个方法,你必须传递一个回调实例。

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

这篇关于如何使用异步改造2.0返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆