【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 )(一)

简介: 【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 )(一)

文章目录

OkHttp 系列文章目录

前言

一、OkHttp 异步 Get 请求

二、OkHttp 同步 Get 请求

三、OkHttp 同步 Post 请求

四、OkHttp 异步 Post 请求

五、完整源代码示例

六、博客资源

前言

在上一篇博客 【OkHttp】Android 项目导入 OkHttp ( 配置依赖 | 配置 networkSecurityConfig | 配置 ViewBinding | 代码示例 ) 中简要介绍了 OkHttp 导入 , 以及同步 Get 请求 ;






一、OkHttp 异步 Get 请求


首先 , 创建 Request 请求对象 ;


     

// Request 中封装了请求相关信息
        Request request = new Request.Builder()
                .url("https://www.baidu.com")   // 设置请求地址
                .get()                          // 使用 Get 方法
                .build();


然后 , 创建异步回调事件 , 即请求完毕后的回调事件 ;


   

// 创建异步回调
        Callback callback = 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();
                Log.i(TAG, "result : " + result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 主线程中执行相关代码
                    }
                });
            }
        };


最后 , 调用 enqueue 方法 , 进行异步 Get 请求操作 ;


   

// 异步 Get 请求
        mOkHttpClient.newCall(request).enqueue(callback);



完整代码如下 :


 

/**
     * OkHttp 异步 Get 请求
     */
    private void httpAsynchronousGet() {
        // Request 中封装了请求相关信息
        Request request = new Request.Builder()
                .url("https://www.baidu.com")   // 设置请求地址
                .get()                          // 使用 Get 方法
                .build();
        // 异步 Get 请求
        mOkHttpClient.newCall(request).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();
                Log.i(TAG, "result : " + result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 主线程中执行相关代码
                    }
                });
            }
        });
    }







二、OkHttp 同步 Get 请求


参考 【OkHttp】Android 项目导入 OkHttp ( 配置依赖 | 配置 networkSecurityConfig | 配置 ViewBinding | 代码示例 ) 三、OkHttp 同步 Get 请求 博客章节 ;



代码示例 : 先初始化 Request 对象 , 然后调用 mOkHttpClient.newCall(request).execute() 进行同步 Get 请求 , 注意同步请求必须在线程中执行 ;


 

/**
     * OkHttp 同步 Get 请求
     */
    private void httpSynchronousGet() {
        // Request 中封装了请求相关信息
        Request request = new Request.Builder()
                .url("https://www.baidu.com")   // 设置请求地址
                .get()                          // 使用 Get 方法
                .build();
        // 同步 Get 请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    response = mOkHttpClient.newCall(request).execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String result = null;
                try {
                    result = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "result : " + result);
            }
        }).start();
    }







三、OkHttp 同步 Post 请求


OkHttp 同步 Post 请求分为 3 33 个步骤 :


① 首先 , 创建 FormBody 对象 , 设置 Post 请求表单 ;


       // 创建 Post 表单 , 主要用于设置 Post 请求键值对

// 创建 Post 表单 , 主要用于设置 Post 请求键值对
        FormBody formBody = new FormBody.Builder()
                .add("Key", "Value")
                .build();

   

② 然后 , 创建 Request 请求对象 , 并传入 FormBody 表单 ;


     

// Request 中封装了请求相关信息
        Request request = new Request.Builder()
                .url("https://www.baidu.com")   // 设置请求地址
                .post(formBody)                          // 使用 Post方法
                .build();


③ 最后 , 进行同步 Post 请求 , 注意要在线程中使用同步 Post 方法 ;


 

// 同步 Get 请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    response = mOkHttpClient.newCall(request).execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String result = null;
                try {
                    result = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "result : " + result);
            }
        }).start();


目录
相关文章
|
8月前
post 的接口请求
post 的接口请求
41 0
|
9月前
|
缓存
POST 为什么会发送两次请求?
POST 为什么会发送两次请求?
557 0
|
10月前
|
前端开发 安全
ajax请求的时候get 和post方式的区别
ajax请求的时候get 和post方式的区别
|
18天前
httprequest- post- get -发送请求
httprequest- post- get -发送请求
13 1
|
14天前
|
XML 安全 前端开发
post为什么会发送两次请求详解
【6月更文挑战第5天】在Web开发中,开发者可能会遇到POST请求被发送了两次的情况,
26 0
|
2月前
|
JSON 中间件 数据格式
在服务器框架中处理 POST 请求
在服务器框架中处理 POST 请求
|
2月前
|
人工智能 前端开发 安全
post为什么会发送两次请求?
post为什么会发送两次请求?
|
8月前
|
JSON 数据格式
OkHttp3发起POST或GET请求
OkHttp3发起POST或GET请求
185 0
|
9月前
|
JSON 小程序 API
【uniapp小程序】request发起请求
【uniapp小程序】request发起请求
127 0
|
Web App开发 网络协议 安全
GET和POST两种基本请求方法的区别
GET和POST两种基本请求方法的区别