博客
关于我
OKHttp开源框架学习一:同步请求总结
阅读量:619 次
发布时间:2019-03-11

本文共 3557 字,大约阅读时间需要 11 分钟。

OkHttp Synchronus Request Guide

Table of Contents

  • [ Versions ](## Versions)
  • [ Reference Articles ](## Reference Articles)
  • [ OkHttp Synchronus Methods Summary ](## OkHttp Synchronus Methods Summary)
  • [ Difference Between Synchronus and Asycnous Requests ](## Difference Between Synchronus and Asycnous Requests)
  • [ Synchronus Request Flow Analysis ](## Synchronus Request Flow Analysis)

Versions

  • Compile com.squareup.okhttp3:okhttp:3.9.0 for OkHttp dependencies

Reference Articles

  • OkHttp Synchronus Methods Summary
  • OkHttp Asycnous Methods Summary

OkHttp Synchronus Methods Summary

  • Create OkHttp and Request objects
  • Wrap Request into a Call object
  • Call execute() method to send synchronus requests
  • Notes:

    • Synchronus requests enter a blocked state until a response is received

    Example Code:

    OkHttpClient mClient = new OkHttpClient.Builder().build();Request request = new Request.Builder().url("http://www.baidu.com").get().build();Call call = mClient.newCall(request);try {    Response response = call.execute();    LogUtils.json(response.body().string());} catch (IOException e) {    e.printStackTrace();}

    OkHttp Asycnous Methods Summary

  • Create OkHttp and Request objects
  • Wrap Request into a Call object
  • Call enqueue() method for asycnous requests
  • Notes:

    • onResponse() and onFailure() callbacks are executed in the worker thread

    Example Code:

    OkHttpClient mClient = new OkHttpClient.Builder().build();Request request = new Request.Builder().url("http://www.baidu.com").get().build();Call call = mClient.newCall(request);call.enqueue(new Callback() {    @Override    public void onFailure(Call call, IOException e) {    }    @Override    public void onResponse(Call call, Response response) throws IOException {        LogUtils.json(response.body().string());        runOnUiThread(new Runnable() {            @Override            public void run() {                tvShow.setText("eeeeee");            }        });    }});

    Difference Between Synchronus and Asycnous Requests

  • Different method calls (execute() vs enqueue())
  • Blocking nature of requests
  • Synchronus Request Flow Analysis

    Step 1: Create OkHttp Client

    OkHttpClient mClient = new OkHttpClient.Builder().build();

    Explanation:

    • Internal Builder class initializes various components like Dispatcher and Connection Pool
    • Connection Pool manages client-server connections and can reuse connections for same URLs

    Step 2: Create Request Object

    Request request = new Request.Builder().url("http://www.baidu.com").get().build();

    Explanation:

    • Builder pattern constructs Request with URL, method, headers, and body

    Step 3: Wrap Request into Call

    Call call = mClient.newCall(request);

    Explanation:

    • Call acts as a bridge between Request and Response-
      implementation handles actual network operations

    Step 4: Execute Synchronus Request

    Call call = mClient.newCall(request);try {    Response response = call.execute();    LogUtils.json(response.body().string());} catch (IOException e) {    e.printStackTrace();}

    Explanation:

    • execute() method sends synchronus request
    • Response handling and logging
    • IOException handling for common network issues

    Dispatcher and Synchronus Flow

    • Dispatcher manages call execution
    • Synchronus calls are added to runningSyncCalls queue
    • Each call execution blocks until completion

    Connection Pool Management

    • Connection Pool optimizes network usage
    • Reuse connections for repeated requests
    • Manage connection lifecycle effectively

    By following this guide, developers can effectively utilize OkHttp's synchronus and asycnous features in their applications, ensuring efficient network communication.

    转载地址:http://puttz.baihongyu.com/

    你可能感兴趣的文章
    phpexcel中文手册
    查看>>
    PHPExcel导入导出 若在thinkPHP3.2中使用(无论实例还是静态调用(如new classname或classname::function)都必须加反斜杠,因3.2就命名空间,如/c...
    查看>>
    phpize及其用法
    查看>>
    phpMailer发送邮件
    查看>>
    PHPMailer发送邮件
    查看>>
    phpmailer发送邮件,可以带附件
    查看>>
    phpmailer的用法
    查看>>
    phpMQTT
    查看>>
    phpmyadmin 安装
    查看>>
    phpmyadmin导出数据库出现Fatal error: Cannot 'break' 2 levels in D:\phpstudy\WWW\phpMyAdmin
    查看>>
    phpmyadmin数据库建表及插入
    查看>>
    phpnow配置
    查看>>
    phprpc简单使用
    查看>>
    phpspider中当爬虫获取数据时如何去掉广告
    查看>>
    phpstorm 2016.3.3 激活
    查看>>
    phpstorm中Xdebug的使用
    查看>>
    phpstorm中使用svn版本控制器
    查看>>
    PHPStorm使用git
    查看>>
    PHPstorm最常用的快捷键,提高开发效率
    查看>>
    Redis五种数据结构
    查看>>