当前位置: 移动技术网 > 移动技术>移动开发>Android > 详解Android中OkHttp3的例子和在子线程更新UI线程的方法

详解Android中OkHttp3的例子和在子线程更新UI线程的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

okhttp用于android的http请求。据说很厉害,我们来一起尝尝鲜。但是使用okhttp也会有一些小坑,后面会讲到如何掉进坑里并爬出来。

首先需要了解一点,这里说的ui线程和主线程是一回事儿。就是唯一可以更新ui的线程。这个只是点会在给okhttp填坑的时候用到。而且,这个内容本身在日常的开发中也经常用到,值得好好学一学。

okhttp发起同步请求

第一个列子是一个同步请求的例子。

private void performsynchttprequest() {
  okhttpclient client = new okhttpclient();

  request request = new request.builder()
    .url("http://www.baidu.com")
    .build();
  call call = client.newcall(request);
  response response = call.execute();
}

但是这样的直接在android的主线程里调用一个网络请求的方法是行不通的,直接抛出ui thread 请求网络的异常。所以我们这里为了可以掩饰要做一点小小的改动。把请求写成同步请求的方式,但是放在一个worker线程里异步的做这个操作。

  private handler requesthandler = new handler() {
    @override
    public void handlemessage(message msg) {
      switch (msg.what) {
        case request_success:
          toast.maketext(mainactivity.this, "successful", toast.length_short).show();
          break;
        case request_fail:
          toast.maketext(mainactivity.this, "request failed", toast.length_short).show();
          break;
        default:
          super.handlemessage(msg);
      }
    }
  };

  private void performsynchttprequest() {
    runnable requesttask = new runnable() {
      @override
      public void run() {
        message msg = requesthandler.obtainmessage();
        try {
          okhttpclient client = new okhttpclient();

          request request = new request.builder()
              .url("http://www.baidu.com")
              .build();
          call call = client.newcall(request);
          // 1
          response response = call.execute();

          if (!response.issuccessful()) {
            msg.what = request_fail;
          } else {
            msg.what = request_success;
          }
        } catch (ioexception ex) {
          msg.what = request_fail;
        } finally {
          // send the message
          // 2
          msg.sendtotarget();
        }
      }
    };

    thread requestthread = new thread(requesttask);
    requestthread.start();
  } 

所以同步的请求都是这么做的response response = call.execute();。

1.发起同步请求之前先新初始化一个okhttpclient。然后是具体的请求,用请求builder来创建这个request。我们这里为了简单url就是*http://www.baidu.com*了。接下来用前面初始化好的client发起一个call:call call = client.newcall(request);。最后执行这个call:response response = call.execute();并获得请求的response。

2.这一部分的可以暂时不要关注。因为这个例子只是为了能以运行起来的方式展示okhttp如何发起同步请求。

okhttp发起异步请求

既然android本身不支持发起同步请求,当然也没人要发起同步请求。这么做是能导致严重的用户体验问题。想象一下,如果你有一个瀑布流,然后瀑布流里全部显示的都是图片。现在用户要不断地往下翻看瀑布流的图片。如果这些图片都用同步请求的话,什么时候可以翻一页不说,系统的anr早就跳出来了。

所以我们就探究一下如何发起一个异步的请求。

  private void performasynchttprequest() {
    okhttpclient client = new okhttpclient();

    request request = new request.builder()
        .url("http://www.baidu.com")
        .build();
    call call = client.newcall(request);
    // 1
    call.enqueue(new callback() {
      // 2
      @override
      public void onfailure(call call, ioexception e) {
        //toast.maketext(mainactivity.this, e.getmessage(), toast.length_short).show();
        
        if (looper.getmainlooper().getthread() == thread.currentthread()) {
          log.d(tag, "main thread");
        } else {
          log.d(tag, "not main thread");
        }
      }

      @override
      public void onresponse(call call, final response response) throws ioexception {
        // 3
        if (looper.getmainlooper().getthread() == thread.currentthread()) {
          log.d(tag, "main thread");
        } else {
          log.d(tag, "not main thread");
        }
      }
    });
  }  

1.同步请求用execute方法,异步就用call.enqueue(new callback()方法。

2.这个callback接口提供了两个方法,一个是onfailure,一个是onresponse。这两个方法分别在请求失败和成功的时候调用。

3.本来一切都似乎应该很简单。网络请求成功或者失败直接在界面更新了。但是木有想到这样会抛异常。然后看了看发现原来onfailure和onresponse两个方法不是在主线程执行。打印出来的log是:okhttp.demo.com.okhttpdemo d/###okhttp: not main thread。

所以要在主线程中更新view只好想别的办法了。在worker线程里更新主线程会抛异常。一般来说有这么几个方法在子线程里更新view。

在子线程更新ui线程

一、activity的runonuithread方法

在activity中有这么一个方法runonuithread。这个方法需要一个runnable实例作为参数。

  mainactivity.this.runonuithread(new runnable() {
    @override
    public void run() {
      log.d(tag, "code: ");
      toast.maketext(mainactivity.this, string.valueof(response.code()), toast.length_short).show();
    }
  });

二、view的post方法

view的post方法也是一样,扔一个runnable的实例进去。然后就在主线程执行了。toast肯定是没有这个方法的。

mainactivity.this.mview.post(new runnable() {
  public void run() {
    log.d("ui thread", "i am the ui thread");
  }
});

三、其他

1.用handler,这个前面的okhttp同步请求的例子可以用。

2.asynctask, 有两个方法可以在主线程中执行:onprogressupdate和onpostexecute。这里我们并不是要更新进度,所以考虑的是后一个方法。

private class backgroundtask extends asynctask<string, void, bitmap> {
  protected void onpostexecute(bitmap result) {
    log.d("ui thread", "i am the ui thread");
  }
}

综合以上,更新ui线程的方法里最后说到的handler方法和asynctask都太重。尤其是asynctask。还要继承实现一堆的方法之后才可以能达到目的,同时还和我们要用的okhttp的使用方法很多不兼容的地方。

所以我们只考虑前面的两种。但是两种方法其实是不一样的。当然,这里并不是说方法的名字不一样。我们来看看android的源代码,这两个方法是如何实现的。

  public final void runonuithread(runnable action) {
    if (thread.currentthread() != muithread) {
      mhandler.post(action);
    } else {
      action.run();
    }
  }
  // mhandler.post(action); 之post方法的实现
  public final boolean post(runnable r) {
    return sendmessagedelayed(getpostmessage(r), 0);
  }

方法runonuithread最后会调用handler的sendmessagedelayed。但是这里只delay了0。也就是方法传到这里的时候会立即执行runonuithread的参数runnable实例会立即执行。

下面看看view的post方法:

  public boolean post(runnable action) {
    final attachinfo attachinfo = mattachinfo;
    if (attachinfo != null) {
      return attachinfo.mhandler.post(action);
    }
    // assume that post will succeed later
    viewrootimpl.getrunqueue().post(action);
    return true;
  }

一般会执行的是viewrootimpl.getrunqueue().post(action);。也就是runnable的实例只是添加到了事件队列中,按照顺序执行。并不一定会立即执行。

我们探讨了那么多,最后就使用runonuithread来更新界面,也就是方法一了。

call.enqueue(new callback() {
  @override
  public void onfailure(call call, ioexception e) {
    final string errormmessage = e.getmessage();

    if (looper.getmainlooper().getthread() == thread.currentthread()) {
      log.d(tag, "main thread");
    } else {
      log.d(tag, "not main thread");
    }

    mainactivity.this.runonuithread(new runnable() {
      @override
      public void run() {
        toast.maketext(mainactivity.this, errormmessage, toast.length_short).show();
      }
    });
  }

  @override
  public void onresponse(call call, final response response) throws ioexception {
    if (looper.getmainlooper().getthread() == thread.currentthread()) {
      log.d(tag, "main thread");
    } else {
      log.d(tag, "not main thread");
    }

    mainactivity.this.runonuithread(new runnable() {
      @override
      public void run() {
        log.d(tag, "code: ");
        toast.maketext(mainactivity.this, string.valueof(response.code()), toast.length_short).show();
      }
    });
  }
});

无论请求成功还是失败,都弹出一个toast。用mainactivity.this.runonuithread在ui线程中弹出这个toast。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网