Okhttp3使用点滴

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);
        }
    });
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 项目要求用json提交参数,以前在网上找的别的大神(忘了是谁)封装的NetUtils工具不适应了,在网上也没找到适...
    微暖_440e阅读 1,160评论 0 0
  • 参考:https://blog.csdn.net/fightingxia/article/details/7094...
    文强小弟阅读 1,219评论 0 1
  • 面试使人能够更正确的认识自己,通过这一轮的面试,我发现自己的基础知识还是有欠缺的。而且我的记性是真不好,很多写...
    kim_liu阅读 584评论 0 2
  • 我们敲代码总是离不开网络请求。网络请求就是想服务器请求数据,一般请求到的数据就是我们列表上要展示数据的数据源。现在...
    Android_HMH阅读 5,166评论 0 3
  • 寒假过的太快了有木有!有木有!有木有童鞋和我的赶脚一样的呢?哈哈! 想想寒假开始之前给自己立下的flag,有点...
    SmileFH阅读 125评论 0 0