博客
关于我
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/

    你可能感兴趣的文章
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>