发表于 2024年03月25日 阅读770次 1条评论
什么是Okhttp
常见的网络请求
- get请求
- 普通的post form请求
Content-Type:application/x-www-form-urlencoded
- 支持问价上传的post form请求
Content-Type:multipart/form-data;boundary=
- post jsoon字符串请求
常用用于APP与server
- 请求中携带自定义header信息
OkHttp的使用流程
- 创建请求:Request.Builder() Request对象
- 通过Request得到Call对象:client.newCall(Request) Call对象
- 执行Call
- 同步 call.execute(
- 异步 call.enqueue()
- 得到Response对象
封装参考
下面是一个封装好的okhttp 类
package com.example.okhttpdemo02;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OKHttpUtil {
private static final OkHttpClient client = new OkHttpClient();
//get 同步请求
public static String getSync(String url) throws Exception{
Request request = new Request.Builder().url(url).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
//post 同步
public static String post(String url, String json) throws Exception {
RequestBody body = RequestBody.create(json, okhttp3.MediaType.get("application/json"));
Request request = new Request.Builder().url(url).post(body).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
//get 异步请求
public static void getAsync(String url, Headers headers, okhttp3.Callback callback) {
Request request = null;
if(headers!=null){
request = new Request.Builder().headers(headers).url(url).build();
}else{
request = new Request.Builder().url(url).build();
}
client.newCall(request).enqueue(callback);
}
//post 异步
public static void postAsync(String url, String json, Headers headers,okhttp3.Callback callback) {
RequestBody body = RequestBody.create(json, okhttp3.MediaType.get("application/json"));
Request request=null;
if(headers !=null){
request = new Request.Builder().url(url).headers(headers).post(body).build();
}else{
request = new Request.Builder().url(url).post(body).build();
}
client.newCall(request).enqueue(callback);
}
}
package com.example.okhttpdemo325;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private OkHttpClient okHttpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okHttpClient =new OkHttpClient();
//get请求
// new Thread(new Runnable() {
// @Override
// public void run() {
// String urlStr="https://postman-echo.com/get?aaa=123&bbb=456";
// Request request = new Request.Builder().url(urlStr).build();
// Call call = okHttpClient.newCall(request);
// try {
// Response response = call.execute();
// JSONObject jsonObject = new JSONObject(response.body().string());
// Log.e("OUT",jsonObject.optJSONObject("args").optString("aaa"));
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// }).start();
//post请求 发送 json数据 MediaType.parse("application/json")
new Thread(new Runnable() {
@Override
public void run() {
String urlStr="https://postman-echo.com/post";
JSONObject jsonObject =new JSONObject();
try {
jsonObject.put("uname1","jack1");
jsonObject.put("uname2","jack2");
jsonObject.put("uname3","jack3");
RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
Request request = new Request.Builder().url(urlStr).post(body).build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
//JSONObject out = new JSONObject(response.body().string());
Log.e("OUT",response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//post请求 发送 form数据
// new Thread(new Runnable() {
// @Override
// public void run() {
// String urlStr="https://postman-echo.com/post";
// try {
// FormBody.Builder builder = new FormBody.Builder();
// builder.add("name1","jacke1");
// builder.add("name2","jacke2");
// RequestBody body = builder.build();
// Request request = new Request.Builder().url(urlStr).post(body).build();
// Call call = okHttpClient.newCall(request);
// Response response = call.execute();
// //JSONObject out = new JSONObject(response.body().string());
// Log.e("OUT",response.body().string());
//
// } catch (Exception e) {
// e.printStackTrace();
// }
//
//
// }
// }).start();
}
}
最后更新时间:4月8日
猜你会喜欢
很好的文章