日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/6/17 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

OkHttp 系列文章目錄

【OkHttp】OkHttp 簡介 ( OkHttp 框架特性 | Http 版本簡介 )
【OkHttp】Android 項目導(dǎo)入 OkHttp ( 配置依賴 | 配置 networkSecurityConfig | 配置 ViewBinding | 代碼示例 )
【OkHttp】OkHttp Get 和 Post 請求 ( 同步 Get 請求 | 異步 Get 請求 | 同步 Post 請求 | 異步 Post 請求 )


文章目錄

  • OkHttp 系列文章目錄
  • 前言
  • 一、OkHttp 異步 Get 請求
  • 二、OkHttp 同步 Get 請求
  • 三、OkHttp 同步 Post 請求
  • 四、OkHttp 異步 Post 請求
  • 五、完整源代碼示例
  • 六、博客資源


前言

在上一篇博客 【OkHttp】Android 項目導(dǎo)入 OkHttp ( 配置依賴 | 配置 networkSecurityConfig | 配置 ViewBinding | 代碼示例 ) 中簡要介紹了 OkHttp 導(dǎo)入 , 以及同步 Get 請求 ;






一、OkHttp 異步 Get 請求



首先 , 創(chuàng)建 Request 請求對象 ;

// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.get() // 使用 Get 方法.build();

然后 , 創(chuàng)建異步回調(diào)事件 , 即請求完畢后的回調(diào)事件 ;

// 創(chuàng)建異步回調(diào)Callback callback = new Callback(){@Overridepublic void onFailure(Call call, IOException e) {// 請求失敗的情況}@Overridepublic void onResponse(Call call, Response response) throws IOException {// 請求成功 , 獲取String result = response.body().string();Log.i(TAG, "result : " + result);runOnUiThread(new Runnable() {@Overridepublic void run() {// 主線程中執(zhí)行相關(guān)代碼}});}};

最后 , 調(diào)用 enqueue 方法 , 進(jìn)行異步 Get 請求操作 ;

// 異步 Get 請求mOkHttpClient.newCall(request).enqueue(callback);

完整代碼如下 :

/*** OkHttp 異步 Get 請求*/private void httpAsynchronousGet() {// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.get() // 使用 Get 方法.build();// 異步 Get 請求mOkHttpClient.newCall(request).enqueue(new Callback(){@Overridepublic void onFailure(Call call, IOException e) {// 請求失敗的情況}@Overridepublic void onResponse(Call call, Response response) throws IOException {// 請求成功 , 獲取String result = response.body().string();Log.i(TAG, "result : " + result);runOnUiThread(new Runnable() {@Overridepublic void run() {// 主線程中執(zhí)行相關(guān)代碼}});}});}



二、OkHttp 同步 Get 請求



參考 【OkHttp】Android 項目導(dǎo)入 OkHttp ( 配置依賴 | 配置 networkSecurityConfig | 配置 ViewBinding | 代碼示例 ) 三、OkHttp 同步 Get 請求 博客章節(jié) ;


代碼示例 : 先初始化 Request 對象 , 然后調(diào)用 mOkHttpClient.newCall(request).execute() 進(jìn)行同步 Get 請求 , 注意同步請求必須在線程中執(zhí)行 ;

/*** OkHttp 同步 Get 請求*/private void httpSynchronousGet() {// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.get() // 使用 Get 方法.build();// 同步 Get 請求new Thread(new Runnable() {@Overridepublic 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 請求分為 333 個步驟 :

① 首先 , 創(chuàng)建 FormBody 對象 , 設(shè)置 Post 請求表單 ;

// 創(chuàng)建 Post 表單 , 主要用于設(shè)置 Post 請求鍵值對FormBody formBody = new FormBody.Builder().add("Key", "Value").build();

② 然后 , 創(chuàng)建 Request 請求對象 , 并傳入 FormBody 表單 ;

// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.post(formBody) // 使用 Post方法.build();

③ 最后 , 進(jìn)行同步 Post 請求 , 注意要在線程中使用同步 Post 方法 ;

// 同步 Get 請求new Thread(new Runnable() {@Overridepublic 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() {// 創(chuàng)建 Post 表單 , 主要用于設(shè)置 Post 請求鍵值對FormBody formBody = new FormBody.Builder().add("Key", "Value").build();// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.post(formBody) // 使用 Post 方法.build();// 同步 Get 請求new Thread(new Runnable() {@Overridepublic 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 請求分為 444 個步驟 :

① 首先 , 創(chuàng)建 FormBody 對象 , 設(shè)置 Post 請求表單 ;

// 創(chuàng)建 Post 表單 , 主要用于設(shè)置 Post 請求鍵值對FormBody formBody = new FormBody.Builder().add("Key", "Value").build();

② 然后 , 創(chuàng)建 Request 請求對象 , 并傳入 FormBody 表單 ;

// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.post(formBody) // 使用 Post方法.build();

③ 在后 , 創(chuàng)建異步 Post 請求的回調(diào)方法 Callback 對象 ;

// 創(chuàng)建異步回調(diào)Callback callback = new Callback(){@Overridepublic void onFailure(Call call, IOException e) {// 請求失敗的情況}@Overridepublic void onResponse(Call call, Response response) throws IOException {// 請求成功 , 獲取String result = response.body().string();Log.i(TAG, "result : " + result);runOnUiThread(new Runnable() {@Overridepublic void run() {// 主線程中執(zhí)行相關(guān)代碼}});}};

④ 最后 , 進(jìn)行同步 Post 請求 , 注意要在線程中使用同步 Post 方法 ;

// 異步 Get 請求mOkHttpClient.newCall(request).enqueue(callback);

完整代碼示例 :

/*** OkHttp 異步 Post 請求*/private void httpAsynchronousPost() {// 創(chuàng)建 Post 表單 , 主要用于設(shè)置 Post 請求鍵值對FormBody formBody = new FormBody.Builder().add("Key", "Value").build();// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.post(formBody) // 使用 Post 方法.build();// 創(chuàng)建異步回調(diào)Callback callback = new Callback(){@Overridepublic void onFailure(Call call, IOException e) {// 請求失敗的情況}@Overridepublic void onResponse(Call call, Response response) throws IOException {// 請求成功 , 獲取String result = response.body().string();Log.i(TAG, "result : " + result);runOnUiThread(new Runnable() {@Overridepublic void run() {// 主線程中執(zhí)行相關(guān)代碼}});}};// 異步 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 客戶端* 注意 : 該類型對象較大, 盡量在應(yīng)用中創(chuàng)建較少的該類型對象* 推薦使用單例*/OkHttpClient mOkHttpClient;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());mOkHttpClient = new OkHttpClient();binding.button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//httpSynchronousGet();//httpAsynchronousGet();//httpSynchronousPost();httpAsynchronousPost();}});}/*** OkHttp 同步 Get 請求*/private void httpSynchronousGet() {// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.get() // 使用 Get 方法.build();// 同步 Get 請求new Thread(new Runnable() {@Overridepublic 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 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.get() // 使用 Get 方法.build();// 創(chuàng)建異步回調(diào)Callback callback = new Callback(){@Overridepublic void onFailure(Call call, IOException e) {// 請求失敗的情況}@Overridepublic void onResponse(Call call, Response response) throws IOException {// 請求成功 , 獲取String result = response.body().string();Log.i(TAG, "result : " + result);runOnUiThread(new Runnable() {@Overridepublic void run() {// 主線程中執(zhí)行相關(guān)代碼}});}};// 異步 Get 請求mOkHttpClient.newCall(request).enqueue(callback);}/*** OkHttp 同步 Post 請求*/private void httpSynchronousPost() {// 創(chuàng)建 Post 表單 , 主要用于設(shè)置 Post 請求鍵值對FormBody formBody = new FormBody.Builder().add("Key", "Value").build();// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.post(formBody) // 使用 Post 方法.build();// 同步 Get 請求new Thread(new Runnable() {@Overridepublic 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() {// 創(chuàng)建 Post 表單 , 主要用于設(shè)置 Post 請求鍵值對FormBody formBody = new FormBody.Builder().add("Key", "Value").build();// Request 中封裝了請求相關(guān)信息Request request = new Request.Builder().url("https://www.baidu.com") // 設(shè)置請求地址.post(formBody) // 使用 Post 方法.build();// 創(chuàng)建異步回調(diào)Callback callback = new Callback(){@Overridepublic void onFailure(Call call, IOException e) {// 請求失敗的情況}@Overridepublic void onResponse(Call call, Response response) throws IOException {// 請求成功 , 獲取String result = response.body().string();Log.i(TAG, "result : " + result);runOnUiThread(new Runnable() {@Overridepublic void run() {// 主線程中執(zhí)行相關(guān)代碼}});}};// 異步 Get 請求mOkHttpClient.newCall(request).enqueue(callback);}}



六、博客資源



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

總結(jié)

以上是生活随笔為你收集整理的【OkHttp】OkHttp Get 和 Post 请求 ( 同步 Get 请求 | 异步 Get 请求 | 同步 Post 请求 | 异步 Post 请求 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。