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

    你可能感兴趣的文章
    Node提示:npm does not support Node.js v12.16.3
    查看>>
    Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
    查看>>
    Node服务在断开SSH后停止运行解决方案(创建守护进程)
    查看>>
    node模块化
    查看>>
    node模块的本质
    查看>>
    node环境下使用import引入外部文件出错
    查看>>
    node环境:Error listen EADDRINUSE :::3000
    查看>>
    Node的Web应用框架Express的简介与搭建HelloWorld
    查看>>
    Node第一天
    查看>>
    node编译程序内存溢出
    查看>>
    Node读取并输出txt文件内容
    查看>>
    node防xss攻击插件
    查看>>
    noi 1996 登山
    查看>>
    noi 7827 质数的和与积
    查看>>
    NOI-1.3-11-计算浮点数相除的余数
    查看>>
    noi.ac #36 模拟
    查看>>
    NOI2010 海拔(平面图最大流)
    查看>>
    NOIp2005 过河
    查看>>
    NOIP2011T1 数字反转
    查看>>
    NOIP2014 提高组 Day2——寻找道路
    查看>>