美文网首页
Okhttp3使用点滴

Okhttp3使用点滴

作者: quanCN | 来源:发表于2019-05-06 17:29 被阅读0次

    OkHttp是一款优秀的HTTP框架,它支持get请求和post请求,支持基于Http的文件上传和下载,支持加载图片,支持下载文件透明的GZIP压缩,支持响应缓存避免重复的网络请求,支持使用连接池来降低响应延迟问题。
    :okhttp需要okio库的支持,okio是okhttp的通信基础
    官网

    安装

    • 引入jar包
      implementation 'com.squareup.okhttp3:okhttp:3.14.2'
      

    同步请求

    • GET请求
      -new OkHttpClient
      -构造Request对象
      -通过前两步中的对象构建Call对象
      -通过 execute() 来提交请求
      OkHttpClient client = new OkHttpClient();
          //Request是OkHttp中访问的请求,Builder是辅助类,Response即OkHttp中的响应。
      String url="http://lilq.cn:8080/quan";
      Request request = new Request.Builder()
              .url(url)
              .build();
      Response response = null;
      try {
          response = client.newCall(request).execute();
          String json = response.body().string();
          //response.body() 不可执行多次 io读取完流关闭
          System.out.println(json);
      } catch (IOException e) {
          e.printStackTrace();
      }
      
    • POST请求
      POST与GET相比,就是在构造Request对象时,需要多构造一个RequestBody对象,用它来携带我们要提交的数据。在构造 RequestBody 需要指定MediaType,用于描述请求/响应 body 的内容类型
      OkHttpClient client = new OkHttpClient();
      String url = "http://lilq.cn:8080/quan";
      String json = "{\"name\":\"Http权威指南\"}";
      //设置请求体 MediaType指定媒体类型
      RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),json);
      Request request = new Request.Builder()
              .url(url)
              .post(body)//等同 method("POST",body) 请求方法需要全部大写
              .build();
      Response response = null;
      try {
          response = client.newCall(request).execute();
          String resJson = response.body().string();
          System.out.println(resJson);
      } catch (IOException e) {
          e.printStackTrace();
      }
      

    异步处理

    异步处理需要通过Call#enqueue(Callback)方法来提交异步请求

    • GET请求
      OkHttpClient client = new OkHttpClient();
      String url = "http://lilq.cn:8080/quan";
      Request request = new Request.Builder()
              .url(url)
              .build();
      //加入队列
      client.newCall(request).enqueue(new Callback() {
          @Override
          public void onFailure(Call call, IOException e) {
              System.out.println(e);
          }
          @Override
          public void onResponse(Call call, Response response) throws IOException {
              String resJson = response.body().string();
              System.out.println(resJson);
          }
      });
      
    • POST请求
      OkHttpClient client = new OkHttpClient();
      String url = "http://lilq.cn:8080/quan";
      String json = "{\"name\":\"JAVA核心思想\"}";
      RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json);
      Request request = new Request.Builder()
              .method("POST",requestBody)
              .url(url)
              .build();
      client.newCall(request).enqueue(new Callback() {
          @Override
          public void onFailure(Call call, IOException e) {
              System.out.println(e);
          }
          @Override
          public void onResponse(Call call, Response response) throws IOException {
              String resJson = response.body().string();
              System.out.println(resJson);
          }
      });
      

    相关文章

      网友评论

          本文标题:Okhttp3使用点滴

          本文链接:https://www.haomeiwen.com/subject/haudoqtx.html