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

    你可能感兴趣的文章
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    nginx添加模块与https支持
    查看>>
    Nginx的Rewrite正则表达式,匹配非某单词
    查看>>
    Nginx的使用总结(一)
    查看>>
    Nginx的是什么?干什么用的?
    查看>>
    Nginx访问控制_登陆权限的控制(http_auth_basic_module)
    查看>>
    nginx负载均衡的五种算法
    查看>>
    Nginx配置ssl实现https
    查看>>
    Nginx配置TCP代理指南
    查看>>
    Nginx配置代理解决本地html进行ajax请求接口跨域问题
    查看>>
    Nginx配置参数中文说明
    查看>>
    Nio ByteBuffer组件读写指针切换原理与常用方法
    查看>>
    NIO Selector实现原理
    查看>>
    NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
    查看>>
    NI笔试——大数加法
    查看>>
    NLP 基于kashgari和BERT实现中文命名实体识别(NER)
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>