【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求

您所在的位置:网站首页 android项目源码百度云 【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求

【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求

2023-04-20 08:43| 来源: 网络整理| 查看: 265

OkHttp 系列文章目

【OkHttp】OkHttp 简介 ( OkHttp 框架特性 | Http 版本简介 ) 【OkHttp】Android 项目导入 OkHttp ( 配置依赖 | 配置 networkSecurityConfig | 配置 ViewBinding | 代码示例 ) 【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

个步骤 :

① 首先 , 创建 FormBody 对象 , 设置 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();

完整代码示例 :

/** * OkHttp 同步 Post 请求 */ private void httpSynchronousPost() { // 创建 Post 表单 , 主要用于设置 Post 请求键值对 FormBody formBody = new FormBody.Builder() .add("Key", "Value") .build(); // Request 中封装了请求相关信息 Request request = new Request.Builder() .url("https://www.baidu.com") // 设置请求地址 .post(formBody) // 使用 Post 方法 .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 请求分为

4

个步骤 :

① 首先 , 创建 FormBody 对象 , 设置 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 请求的回调方法 Callback 对象 ;

// 创建异步回调 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() { // 主线程中执行相关代码 } }); } };

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

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

完整代码示例 :

/** * OkHttp 异步 Post 请求 */ private void httpAsynchronousPost() { // 创建 Post 表单 , 主要用于设置 Post 请求键值对 FormBody formBody = new FormBody.Builder() .add("Key", "Value") .build(); // Request 中封装了请求相关信息 Request request = new Request.Builder() .url("https://www.baidu.com") // 设置请求地址 .post(formBody) // 使用 Post 方法 .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() { // 主线程中执行相关代码 } }); } }; // 异步 Get 请求 mOkHttpClient.newCall(request).enqueue(callback); }五、完整源代码示例package com.example.okhttp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import com.example.okhttp.databinding.ActivityMainBinding; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; /** * ViewBinding 类 * activity_main 布局映射出来的类 * 该类主要作用是封装组件的获取 */ ActivityMainBinding binding; /** * OkHttp 客户端 * 注意 : 该类型对象较大, 尽量在应用中创建较少的该类型对象 * 推荐使用单例 */ OkHttpClient mOkHttpClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); mOkHttpClient = new OkHttpClient(); binding.button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //httpSynchronousGet(); //httpAsynchronousGet(); //httpSynchronousPost(); httpAsynchronousPost(); } }); } /** * 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 异步 Get 请求 */ private void httpAsynchronousGet() { // 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() { // 主线程中执行相关代码 } }); } }; // 异步 Get 请求 mOkHttpClient.newCall(request).enqueue(callback); } /** * OkHttp 同步 Post 请求 */ private void httpSynchronousPost() { // 创建 Post 表单 , 主要用于设置 Post 请求键值对 FormBody formBody = new FormBody.Builder() .add("Key", "Value") .build(); // Request 中封装了请求相关信息 Request request = new Request.Builder() .url("https://www.baidu.com") // 设置请求地址 .post(formBody) // 使用 Post 方法 .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 请求 */ private void httpAsynchronousPost() { // 创建 Post 表单 , 主要用于设置 Post 请求键值对 FormBody formBody = new FormBody.Builder() .add("Key", "Value") .build(); // Request 中封装了请求相关信息 Request request = new Request.Builder() .url("https://www.baidu.com") // 设置请求地址 .post(formBody) // 使用 Post 方法 .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() { // 主线程中执行相关代码 } }); } }; // 异步 Get 请求 mOkHttpClient.newCall(request).enqueue(callback); } }六、博客资源

GitHub : https://github.com/han1202012/OkHttp



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3