Okhttp学习

基本的使用

Android使用注意申请网络权限,同时不能在主线程请求

gradle配置 :

compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.okio:okio:1.11.0'

超时设置

    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.readTimeout(10, TimeUnit.SECONDS);
    builder.writeTimeout(10, TimeUnit.SECONDS);
    builder.connectTimeout(10, TimeUnit.SECONDS);
    builder.pingInterval(10, TimeUnit.SECONDS);// websocket 轮训间隔

设置缓存

private final String HTTP_CACHE_FILENAME = "HttpCache";
private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
    @SuppressLint("DefaultLocale")
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response originalResponse = chain.proceed(request);
        String cache = request.header("Cache-Time");
        String demo = request.header("demo");
        Log.d("MainActivity", "cache: " + cache);
        Log.d("MainActivity", "demo: " + demo);
        if (!TextUtils.isEmpty(cache)) {
            return originalResponse.newBuilder()
                    .removeHeader("Pragma")
                    .removeHeader("Cache-Control")
                    .header("Cache-Control", "max-age=" + cache)
                    .build();
        } else {
            return originalResponse;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.request).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gohttpRequest();
        }
    });
}

public void gohttpRequest() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    File httpCacheDirectory = new File(getCacheDir(), HTTP_CACHE_FILENAME);
    Log.d("MainActivity", "Environment.getExternalStorageDirectory():" + Environment.getRootDirectory());
    //设置缓存
    Cache cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
    builder.cache(cache);
    builder.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR);
    OkHttpClient client = builder.build();

    Request request = new Request.Builder()
            .url("http://www.publicobject.com/helloworld.txt")
            .header("Cache-Time", "3600*24")
            .header("demo", "okhttp")
            .build();

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

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Headers headers = response.headers();
            Log.d("MainActivity", "headers: " + headers.toString());
            Log.d("MainActivity", "response message:" + response.message());
            if (response.isSuccessful())
                Log.d("MainActivity", "response cacheResponse: " + response.cacheResponse());
            response.body().close();
        }
    });
}

Cookie保存

OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.cookieJar(new CookieJar() {
        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
            // 保存cookie通常使用SharedPreferences
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            return null;
        }
    });

拦截器

  1. 请求转发:
    即request.getRequestDispatcher().forward(),是一种服务器的行为,客户端只有一次请求,服务器端转发后会将请求对象保存,地址栏中的URL地址不会改变,得到响应后服务器端再将响应发给客户端;

  2. 请求重定向:
    即response.sendRedirect(),是一种客户端行文,从本质上讲等同于两次请求,前一次请求对象不会保存,地址栏的URL地址会改变。

用生活中一个常见的例子来说明两者之间的区别,某人要去办理护照:
转发:某人去了甲局,甲局看了之后,直到护照应该由乙局来管,但甲局的工作人员并没有赶走某人,而是让某人等着,自己在办公室后面联系了乙局的工作人员,乙局护照办好后送到了甲局,然后甲局的工作人员将护照交给了某人;
重定向:某人去了甲局后,甲局的工作人员说护照不归他们关,应该去乙局。然后某人自己去了乙局,办了护照。

网络拦截器

public final class LoggingInterceptors {
  private static final Logger logger = Logger.getLogger(LoggingInterceptors.class.getName());
  private final OkHttpClient client = new OkHttpClient.Builder()
      .addNetworkInterceptor(new LoggingInterceptor())//网络拦截器
      .build();

  public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://www.publicobject.com/helloworld.txt")
        .build();

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

  private static class LoggingInterceptor implements Interceptor {
    @Override public Response intercept(Chain chain) throws IOException {
      long t1 = System.nanoTime();
      Request request = chain.request();
      logger.info(String.format("Sending request %s on %s%n%s",
          request.url(), chain.connection(), request.headers()));
      Response response = chain.proceed(request);

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

  public static void main(String... args) throws Exception {
    new LoggingInterceptors().run();
  }
}

Once for the initial request to http://www.publicobject.com/helloworld.txt, and another for the redirect to https://publicobject.com/helloworld.txt.

第一次初始化请求http://www.publicobject.com/helloworld.txt,第二次重定向到https://publicobject.com/helloworld.txt

进行了两次请求

如果将上面的地址改为

 .url("https://publicobject.com/helloworld.txt")
只会进行一次请求

应用拦截器

将上面的   addNetworkInterceptor(new LoggingInterceptor())//网络拦截器 改为

addInterceptor(new LoggingInterceptor())

只进行一次请求,没有进行重定向

Https

github地址 : https://github.com/wangtao520/HttpsDemo

GET

   OkHttpClient.Builder builder = new OkHttpClient.Builder();
   OkHttpClient client = builder.build();

   Request.Builder reBuilder = new Request.Builder();
   reBuilder.url("https://www.baidu.com");
   reBuilder.method("GET", null);

   client.newCall(reBuilder.build()).enqueue(new Callback() {
       @Override
       public void onFailure(Call call, IOException e) {
           System.out.println(e.toString());
       }

       @Override
       public void onResponse(Call call, Response response) throws IOException {
           String s = response.body().toString();
           System.out.println(s);
       }
   });

POST

OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .build();

// 2.创建请求参数,注意,此处有多种方式
RequestBody requestBody = new FormBody.Builder()
    .add("param", "value")
    .build();
// 3.创建请求request
Request request = new Request.Builder()
    .url("https://wwww.xxx.com")
    .post(requestBody)
    .build();
// 4.发起请求,此处使用的是异步请求,按需要选择同步或异步
okHttpClient.newCall(request)
    .enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
// 5.处理相响应
        }
    });

上传文件

// 1.创建OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .build();

// 不同点
// 2.创建请求参数,设置对应的参数类型即可
 RequestBody requestBody = RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), new File("xxx.txt"));
// 3.创建请求request
Request request = new Request.Builder()
    .url("https://wwww.xxx.com")
    .post(requestBody)
    .build();
// 4.发起请求
okHttpClient.newCall(request)
    .enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
// 5.处理相响应
        }
    });

Multipart文件

// 1.创建OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .build();

// 不同点
// 2.创建请求参数,设置对应的参数类型即可
RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("fileParam", "xxx.png", RequestBody.create(MediaType.parse("image/png"), new File("xxx/xxx.png")))
            .addFormDataPart("param", "value")
            .build();
// 3.创建请求request
Request request = new Request.Builder()
    .url("https://wwww.xxx.com")
    .post(requestBody)
    .build();
// 4.发起请求
okHttpClient.newCall(request)
    .enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
// 5.处理相响应
        }
    });

Websocket

okhttp3支持websocket,如果不了解请自行搜索,简易推送,轮训都可以使用。

websocket协议首先会发起http请求,握手成功后,转换协议保持长连接,类似心跳

// 方法可以选择实现
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .build();
Request request = new Request.Builder()
    .url("https://wwww.xxx.com")
    .build();
okHttpClient.newWebSocket(request, new WebSocketListener() {
@Override
    public void onMessage(WebSocket webSocket, String text) {
        super.onMessage(webSocket, text);
      // 当收到文本消息
    }

    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        super.onOpen(webSocket, response);
       // 连接成功
    }

    @Override
    public void onMessage(WebSocket webSocket, ByteString bytes) {
        super.onMessage(webSocket, bytes);
       // 收到字节消息,可转换为文本
    }

    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        super.onClosed(webSocket, code, reason);
       // 连接被关闭
    }

    @Override
    public void onFailure(WebSocket webSocket, Throwable t, Response response) {
        super.onFailure(webSocket, t, response);
       // 连接失败
    }
});

混淆

okhttp:

-dontwarn okhttp3.**

okio:

-dontwarn okio.**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值