1.Okhttp

接入

OkHttp在3.13.x以上的版本需要在Android 5.0+ (API level 21+)和Java 1.8的环境开发。

同时还需要再添加Okio的依赖库,而Okio在1.x版本是基于Java实现的,2.x则是Kotlin实现的。

dependencies {
    //...
    //OkHttp
    implementation 'com.squareup.okhttp3:okhttp:3.14.2'
    implementation 'com.squareup.okio:okio:1.17.4'
}

Get请求

请求分为同步请求和异步请求,先看看同步请求  利用execute方法

public void getSyn(final String url) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //创建OkHttpClient对象
                OkHttpClient client = new OkHttpClient();
                //创建Request
                Request request = new Request.Builder()
                        .url(url)//访问连接
                        .get()
                        .build();
                //创建Call对象        
                Call call = client.newCall(request);
                //通过execute()方法获得请求响应的Response对象        
                Response response = call.execute();
                if (response.isSuccessful()) {
                    //处理网络请求的响应,处理UI需要在UI线程中处理
                    //...
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

这就是一段同步Get请求的代码,同步网络请求需要在子线程中执行,而处理UI需要回到UI线程中处理。

在看看Get的异步请求,这时就不需要自己创建子线程了,但是处理UI同样需要在UI线程中处理,不能再请求响应的回调方法中处理  利用enqueue方法

public void getAsyn(String url) {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            //...
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if(response.isSuccessful()){
                String result = response.body().string();
                //处理UI需要切换到UI线程处理
            }
        }
    });
}

看了两种不同的Get请求,基本流程都是先创建一个OkHttpClient对象,然后通过Request.Builder()创建一个Request对象,OkHttpClient对象调用newCall()并传入Request对象就能获得一个Call对象。而同步和异步不同的地方在于execute()enqueue()方法的调用,调用execute()为同步请求并返回Response对象;调用enqueue()方法测试通过callback的形式返回Response对象。

注意:无论是同步还是异步请求,接收到Response对象时均在子线程中,其中通过Response对象获取请求结果需要在子线程中完成,在得到结果后再切换到UI线程改变UI

Post请求

Post请求与Get请求不同的地方在于Request.Builderpost()方法,post()方法需要一个RequestBody的对象作为参数

public void post(String url,String key,String value){
    OkHttpClient client = new OkHttpClient();
    FormBody body = new FormBody.Builder()
            .add(key,value)
            .build();
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            //...
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if(response.isSuccessful()){
                String result = response.body().string();
                //处理UI需要切换到UI线程处理
            }
        }
    });
}

RequestBody是一个抽象类,分别有FormBodyMultipartBody两个子类,上面这个例子使用的是FormBody,用于传输表单类型的参数。MultipartBody则支持多类型的参数传递,例如:在传输表单类型的参数的同时,还是可以传输文件。创建一个MultipartBody对象再调用post()方法就OK了。

MultipartBody body = new MultipartBody.Builder()
//      添加表单参数
//      .addFormDataPart(key,value)
        .addFormDataPart(name, fileName, RequestBody.create(MediaType.get("application/octet-stream"), file))
        .build();

设置超时时间

OkHttp可以设置调用、连接和读写的超时时间,都是通过OkHttpClient.Builder设置的。如果不主动设置,OkHttp将使用默认的超时设置。

OkHttpClient mClient = new OkHttpClient.Builder()
        .callTimeout(6_000, TimeUnit.MILLISECONDS)
        .connectTimeout(6_000, TimeUnit.MILLISECONDS)
        .readTimeout(20_000, TimeUnit.MILLISECONDS)
        .writeTimeout(20_000, TimeUnit.MILLISECONDS)
        .build();

设置请求Header

请求的Header是通过Request.Builder对象的相关方法来维护的,如下:

  • headers(Headers headers)
  • header(String name, String value)
  • addHeader(String name, String value)
  • removeHeader(String name)

addHeaderremoveHeader方法比较好理解,分别是添加和移除header信息。header(String name, String value)这是会重新设置指定name的header信息。

headers(Headers headers)则是会移除掉原有的所有header信息,将参数headers的header信息添加到请求中。这是这几个方法的一些差别。

使用的话都是Builder模式的链式调用,举个栗子

Request request = new Request.Builder()
        .header("Accept","image/webp")
        .addHeader("Charset","UTF-8")
        .url(url)
        .build();

Interceptors(拦截器)

拦截器是OkHttp当中一个比较强大的机制,可以监视、重写和重试调用请求。

这是一个比较简单的Interceptor的实现,对请求的发送和响应进行了一些信息输出。

class LoggingInterceptor implements Interceptor {
    public static final String TAG = "Http_log";

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request request = chain.request();

        long t1 = System.nanoTime();
        Log.i(TAG, String.format("Sending request %s on %s%n%s",
                request.url(), chain.connection(), request.headers()));

        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        Log.i(TAG, String.format("Received response for %s in %.1fms%n%s",
                response.request().url(), (t2 - t1) / 1e6d, response.headers()));

        return response;
    }
}

需要实现其中intercept(Interceptor.Chain chain)方法,同时必须调用chain.proceed(request)代码,也就是网络请求真正发生的地方。

拦截器可以设置多个,并且拦截器的调用是有顺序的。官网举的例子是,同时添加一个压缩拦截器和一个校验拦截器,需要决定数据是先被压缩在校验,还是先校验在压缩。

拦截器还分为应用拦截器(Application Interceptors)和网络拦截器(Network Interceptors)

 上图可以看出应用拦截器是处于应用和OkHttp核心之间,而网络拦截器则是在OkHttp核心与网络之间,这里就直接搬运官网的示例,使用LoggingInterceptor来看一下两种拦截器的差异。

Application Interceptors

先看看应用拦截器,通过OkHttpClient.BuilderaddInterceptor方法添加拦截器

OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new LoggingInterceptor())
    .build();

Request request = new Request.Builder()
    .url("http://www.publicobject.com/helloworld.txt")
    .header("User-Agent", "OkHttp Example")
    .build();

Response response = client.newCall(request).execute();
response.body().close();

看请求和响应的两个链接是不同的,URL http://www.publicobject.com/helloworld.txt会重定向到 https://publicobject.com/helloworld.txt,OkHttp会自动跟随重定向,而应用拦截器只被调用一次,并且chain.proceed()返回的Response对象是具有重定向响应对象。

INFO: Sending request http://www.publicobject.com/helloworld.txt on null
User-Agent: OkHttp Example

INFO: Received response for https://publicobject.com/helloworld.txt in 1179.7ms
Server: nginx/1.4.6 (Ubuntu)
Content-Type: text/plain
Content-Length: 1759
Connection: keep-alive

Network Interceptors

再来看看网络拦截器,通过OkHttpClient.BuilderaddNetworkInterceptor方法添加拦截器

OkHttpClient client = new OkHttpClient.Builder()
    .addNetworkInterceptor(new LoggingInterceptor())
    .build();

Request request = new Request.Builder()
    .url("http://www.publicobject.com/helloworld.txt")
    .header("User-Agent", "OkHttp Example")
    .build();

Response response = client.newCall(request).execute();
response.body().close();

结果日志:

INFO: Sending request http://www.publicobject.com/helloworld.txt on Connection{www.publicobject.com:80, proxy=DIRECT hostAddress=54.187.32.157 cipherSuite=none protocol=http/1.1}
User-Agent: OkHttp Example
Host: www.publicobject.com
Connection: Keep-Alive
Accept-Encoding: gzip

INFO: Received response for http://www.publicobject.com/helloworld.txt in 115.6ms
Server: nginx/1.4.6 (Ubuntu)
Content-Type: text/html
Content-Length: 193
Connection: keep-alive
Location: https://publicobject.com/helloworld.txt

INFO: Sending request https://publicobject.com/helloworld.txt on Connection{publicobject.com:443, proxy=DIRECT hostAddress=54.187.32.157 cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA protocol=http/1.1}
User-Agent: OkHttp Example
Host: publicobject.com
Connection: Keep-Alive
Accept-Encoding: gzip

INFO: Received response for https://publicobject.com/helloworld.txt in 80.9ms
Server: nginx/1.4.6 (Ubuntu)
Content-Type: text/plain
Content-Length: 1759
Connection: keep-alive

从日志来看,拦截器运行了两次,第一次请求了http://www.publicobject.com/helloworld.txt,第二次则是重定向到https://publicobject.com/helloworld.txt。同时通过网络拦截能获得更多的header信息。更多的关于Interceptor的使用以及它们各自的优缺点,请参考OkHttp官方说明文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值