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

    你可能感兴趣的文章
    ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
    查看>>
    ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
    查看>>
    ntp server 用法小结
    查看>>
    ntpdate 通过外网同步时间
    查看>>
    ntpdate同步配置文件调整详解
    查看>>
    NTPD使用/etc/ntp.conf配置时钟同步详解
    查看>>
    NTP及Chrony时间同步服务设置
    查看>>
    NTP服务器
    查看>>
    NTP配置
    查看>>
    NUC1077 Humble Numbers【数学计算+打表】
    查看>>
    NuGet Gallery 开源项目快速入门指南
    查看>>
    NuGet(微软.NET开发平台的软件包管理工具)在VisualStudio中的安装的使用
    查看>>
    nuget.org 无法加载源 https://api.nuget.org/v3/index.json 的服务索引
    查看>>
    Nuget~管理自己的包包
    查看>>
    NuGet学习笔记001---了解使用NuGet给net快速获取引用
    查看>>
    nullnullHuge Pages
    查看>>
    NullPointerException Cannot invoke setSkipOutputConversion(boolean) because functionToInvoke is null
    查看>>
    null可以转换成任意非基本类型(int/short/long/float/boolean/byte/double/char以外)
    查看>>
    Number Sequence(kmp算法)
    查看>>
    Numix Core 开源项目教程
    查看>>