okhttp3 使用讲解

2019-01-07  本文已影响0人  NullPoint3Exce

HTTP/2 support allows all requests to the same host to share a socket.
Connection pooling reduces request latency (if HTTP/2 isn’t available).
Transparent GZIP shrinks download sizes.

OkHttp supports Android 5.0+ (API level 21+) and Java 8+.

  implementation 'com.squareup.okhttp3:okhttp:3.2.0'

  <uses-permission android name = "android.permission.INTERNET"/>

           OkHttpClient okHttpClient = new OkHttpClient();
           Request request = new Request.Builder()
                   .url(home_list_url)
                   .build();
           Call call = okHttpClient.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 result = response.body().string();
               }
           });

       } catch (Exception e) {
           e.printStackTrace();
       }
        try {
            //post方式提交的数据
            FormBody formBody = new FormBody.Builder()
                    .add("username", "android")
                    .add("password", "50")
                    .add("phone", "50")
                    .add("verify", "50")
                    .build();
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    .post(formBody)
//                    .header("User-Agent", "my-agent")
//                    .addHeader("Accept-Language", "zh-cn")
                    .url(post_url)
                    .build();
            Call call = okHttpClient.newCall(request);

            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.d(TAG, "onFailure: ");
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final String result = response.body().string();
                    Log.d(TAG, "onResponse: " + result);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(result);
                        }
                    });
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
RetryAndFollowUpIntercaptors
Bridgeinterceptors
Cacheinterceptors
Connectinterceptors
CallserverIntercaptors

一个完整的异步请求,主要有以下几点:new okhttpclient new Request(),通过client.newCall,传入request请求对象且返回一个Call对象,执行call.enqueue()来实现一个完整的请求逻辑。主要涉及几点:
1.构建okhttpclient对象的时候,会new Dispatcher()对象,Dispatcher主要用于维护同步和异步请求的状态。并维护一个线程池,有三个集合,一个异步等待集合,一个异步运行集合,一个是同步运行集合。
2.RealCall 对call接口的实现类,封装了请求服务器的数据和配置项目,同时处理执行具体同步和异步的操作。
3.interceptors 拦截器,一个完整的请求会依次执行以下几个拦截器,最终返回结果。
RetryAndFollowUpIntercaptors 重试和重定向拦截器
Bridgeinterceptors 桥接拦截器
Cacheinterceptors 缓存拦截器
Connectinterceptors 链接拦截器
CallserverIntercaptors 请求服务拦截器

上一篇 下一篇

猜你喜欢

热点阅读