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
3
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
4
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);
}
}