彻底掌握网络通信(十六)走进OkHttp3的世界(一)引言

彻底掌握网络通信(一)Http协议基础知识
彻底掌握网络通信(二)Apache的HttpClient基础知识
彻底掌握网络通信(三)Android源码中HttpClient的在不同版本的使用
彻底掌握网络通信(四)Android源码中HttpClient的发送框架解析
彻底掌握网络通信(五)DefaultRequestDirector解析
彻底掌握网络通信(六)HttpRequestRetryHandler解析
彻底掌握网络通信(七)ConnectionReuseStrategy,ConnectionKeepAliveStrategy解析
彻底掌握网络通信(八)AsyncHttpClient源码解读
彻底掌握网络通信(九)AsyncHttpClient为什么无法用Fiddler来抓包
彻底掌握网络通信(十)AsyncHttpClient如何发送JSON解析JSON,以及一些其他用法
彻底掌握网络通信(十一)HttpURLConnection进行网络请求的知识准备
彻底掌握网络通信(十二)HttpURLConnection进行网络请求概览
彻底掌握网络通信(十三)HttpURLConnection进行网络请求深度分析
彻底掌握网络通信(十四)HttpURLConnection进行网络请求深度分析二:缓存
彻底掌握网络通信(十五)HttpURLConnection进行网络请求深度分析三:发送与接收详解

从这章开始,我们将开始OkHttp之旅,在旅行的路上,我们可以领略下这个网络通信工具是如何工作的

  1. 首先简单介绍下 OkHttp

okhttp是一个流行的客户端网络请求库,其基于httpclient和httpurlconncect来完成网络通信,如post,get,文件上传/下载;其 github地址 https://github.com/square/okhttp


从github上我们下载下来的okhttp源码结构如下
这里写图片描述

从CHANGELOG.md文件中,我们可以看出版本的变化,目前最新的版本号为V 3.11.0

疑问一: 那在Android Studio中是不是可以引入V 3.11.0 版本的Okhttp

我们知道在Android Studio中引入OkHttp的方式如下

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compile 'com.squareup.okhttp:okhttp:2.7.5'
}

我们通过在dependencies中添加’com.squareup.okhttp:okhttp:2.7.5’即可链如okhttp。如果此处你将版本号写为:3.11.0这样可以吗?

答案是否; 因为在jcenter中,并没有这个3.11.0版本,顾你无法链接;

备注:android studio中有两种文件仓库,jcenter和Maven Central,这两个文件仓库可供第三发开发者上传自己的库作用,我们在项目中使用的库都是从这两个文件仓库中下载下来的;

疑问二:我们怎么确认在jcenter中OkHttp的最新版本
1. 进入jcenter官网
2. 在下图中输入”com.squareup.okhttp:okhttp”,即可查看到当前okhttp最新版本号为2.7.5

这里写图片描述


疑问三:okhttp似乎有两个版本,一个是okhttp,另一个是okhttp3,这两者有什么区别?github上的是哪个版本

是的,早期的okhttp的版本即okhttp,最新的版本为okhttp3,我们在github官网下载的源码其实是okhttp3 ,其最新的版本是3.11.0;顾在开发中我建议大家配置okhttp3来作为网络框架,即使用如下代码引入okhttp3

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compile 'com.squareup.okhttp3:okhttp:3.11.0'
}

okhttp和okhtp3主要的区别就是在封装,okhttp3采用更为先进的设计模式,来对网络请求进行封装,代码结构更为清晰;而且目前维护的也是okhttp3,所以我们的项目中建议采用okhttp3


  1. 再看下如何利用 OkHttp发送get 和 post请求, 借此代码,我们将会在下篇介绍下okhttp的整体框架和源码分析


    Get请求
    private void getRequest() {
        OkHttpClient mOkHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://112.4.3.136:8080/portalone/homesdk/NetTVUniLogin")
                .addHeader("name", "test")
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
            }
        });
    }



Post请求

    private void postRequest() {
        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("name", "test")
                .build();

        Request request = new Request.Builder()
                .url("http://112.4.3.136:8080/portalone/homesdk/NetTVUniLogin")
                .addHeader("Accept", "application/json; q=0.5")
                .post(formBody)
                .build();

        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("hwj", "**postRequest onResponse**" + str);
            }
        });
    }

综述:利用OKhttp3进行网络请求主要的类有OkHttpClient,RequestBody,Request ,Call 和 Response ;
问题:他们具体是什么作用,OkHttp3是如何进行封装,这些我们将在下篇介绍

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值