美文网首页
接口回调

接口回调

作者: Longmaxie | 来源:发表于2016-08-11 18:28 被阅读48次

知乎大神:
你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货。
在这个例子里,你的电话号码就叫回调函数,
你把电话留给店员就叫登记回调函数,
店里后来有货了叫做触发了回调关联的事件,
店员给你打电话叫做调用回调函数,
你到店里去取货叫做响应回调事件。

好处: 降低代码的耦合性,使代码更灵活、简洁
用法:
1.定义回调接口:

/** 
   * 网络请求回调接口 
 */
public interface HttpCallBackListener { 
          void onFinish(String respose); 
          void onError(Exception e);
}
  1. 定义回调函数(将接口作为参数)
// 网络请求工具类
public class HttpUtil { 
      public static void requestData(final String urlStr, final HttpCallBackListener listener) { 
          new Thread(new Runnable() { 
          @Override 
          public void run() { 
              HttpURLConnection connection = null; 
              try { 
                   URL url = new URL(urlStr); 
                   connection = (HttpURLConnection) url.openConnection();                                      connection.setRequestMethod("GET"); 
                  connection.setConnectTimeout(8000); 
                  connection.setReadTimeout(8000); 
                  connection.setDoInput(true); 
                  connection.setDoOutput(true); 
                  InputStream in = connection.getInputStream(); 
                  BufferedReader br = new BufferedReader(new InputStreamReader(in));
                  StringBuilder sb = new StringBuilder(); 
                  String line; 
                  while ((line = br.readLine()) != null) { 
                        sb.append(line); 
                   }
                   if (listener != null) { 
                              //回调onFinish方法 listener.onFinish(sb.toString()); 
                    } 
                } catch (Exception e) {
                      if (listener != null) {
                        //回调onError方法 listener.onError(e); 
                } 
            } finally { 
                    if (connection != null) { 
                             connection.disconnect();
                      }
            } } }).start(); }}
  1. 使用回调方法
onCreate() {
 HttpUtil.requestData("请求的网址", new HttpCallBackListener() { 
 @Override 
 public void onFinish(String respose) { //处理请求 } 
 @Override 
 public void onError(Exception e) { //处理异常 } });
}
  1. 使用回调方法2
public class MainActivity extends AppCompatActivity implements HttpCallBackListener { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
           super.onCreate(savedInstanceState); 
           setContentView(R.layout.activity_main); 
           HttpUtil.requestData("请求的网址", this); } 
   @Override 
   public void onFinish(String respose) { //处理请求 } 
   @Override
   public void onError(Exception e) { //处理异常 }}

相关文章

  • Kotlin使用接口回调

    1.Java中的接口回调实现(支持多方法回调) 声明回调接口,初始化接口 使用接口回调(无参数) 使用接口回调(带...

  • Java回调深入理解

    1 接口回调 1.1 接口回调概念 什么是接口回调接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声...

  • Android Module之间数据传递

    方法一:使用接口回调 (1)在子module创建回调接口(参数可变) (2)在子module 实现类设置接口回调 ...

  • Android接口回调

    之前对接口回调一直有点模糊,会写但是理解的不透彻,今天记录一下自己理解的回调是什么。 接口回调是什么? 接口回调是...

  • Kotlin简单回调接口(lambda实现)

    注:适用于回调接口单个方法 1.方法无参无返回值回调 (1)声明回调接口,以及初始化接口 (2)接口方法的调用 (...

  • kotlinInterface

    注:适用于回调接口单个方法 1.方法无参无返回值回调 (1)声明回调接口,以及初始化接口 2.方法有参无返回值回调...

  • Fragment向Activity传递值

    一.Fragment向Activity传值的步骤 接口回调传递(5部曲) fragment中准备回调接口 接口中...

  • 接口回调

    实现fragment往Activity传值 接口回调是指:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接...

  • 接口回调

    接口A: 类B: 类C:

  • 接口回调

    接口回调,其实就是将接口里面的内容(可能是接口里面数据需要传递),传递到实现这个接口的类中,看下面的是一个Asyn...

网友评论

      本文标题:接口回调

      本文链接:https://www.haomeiwen.com/subject/rsjssttx.html