目录
1.同步和异步
1.同步 GET 的意思是一直等待 http 请求 , 直到返回了响应 . 在这之间会阻塞进程 , 所以通过 get 不能在
Android 的主线程中执行 , 否则会报错 . 对于同步请求在请求时需要开启子线程,请求成功后需要跳转到UI 线程修改 UI 。
2.异步 GET 是指在另外的工作线程中执行 http 请求 , 请求时 不会阻塞 当前的线程 , 所以可以 Android 主线程中使用. 这种方式不用再次开启子线程,但回调方法是执行在子线程中,所以在更新UI 时还要跳转到 UI 线程中。
2.OKHTTP同步Get使用步骤
第一步:添加依赖
implementation("com.squareup.okhttp3:okhttp:4.10.0")
第二步:设置网络权限
在AndroidManifest.xml添加Internet
<uses-permission android:name="android.permission.INTERNET" />
第三步:进行网络请求
1. 创建线程
2. 创建客户端
3. 创建请求
4. 使用客户端发送请求
5. 处理返回结果
new Thread(new Runnable() {
@Override
public void run() {
//创建客户端
OkHttpClient httpClient=new OkHttpClient();
//发送请求
Request request=new Request.Builder()
.url("https://api.vvhan.com/api/girl?type=json")
.build();
//使用客户端发送请求
try (Response response=httpClient.newCall(request).execute()) {
zhi=response.body().string();
handler.sendEmptyMessage(0x111);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
第四步:Handler
public Handler handler=new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==0x111){
try {
JSONObject object=new JSONObject(zhi);
String imgurl=object.getString("imgurl");
Glide.with(MainActivity.this).load(imgurl).into(iv_show);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
3.OKHTTP的异步GET使用步骤
第一步:添加依赖
implementation("com.squareup.okhttp3:okhttp:4.10.0")
第二步:设置网络权限
在AndroidManifest.xml添加Internet
<uses-permission android:name="android.permission.INTERNET" />
第三步:进行网络请求
1. 创建客户端
2. 创建请求
3. 使用客户端发送异步请求
4. 创建回调方法
5. 处理返回结果
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder()
.url("https://api.vvhan.com/api/girl?type=json")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
zhi=response.body().string();
handler.sendEmptyMessage(0x111);
}
});
第四步:Handler
public Handler handler=new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==0x111){
try {
JSONObject object=new JSONObject(zhi);
String imgurl=object.getString("imgurl");
Glide.with(MainActivity.this).load(imgurl).into(iv_show);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
};
3.OKHTTP的同步POST使用步骤
第一步:添加依赖
implementation("com.squareup.okhttp3:okhttp:4.10.0")
第二步:设置网络权限
在 AndroidManifest.xml 添加 Internet
<uses-permission android:name="android.permission.INTERNET" />
第三步:进行网络请求
1. 创建线程
2. 创建客户端
3. 构建请求体,将传递参数放到请求体中
4. 创建请求
5. 使用客户端发送请求
6. 处理返回结果
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client=new OkHttpClient();
MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
RequestBody body=RequestBody.Companion.create(param,mediaType);
Request request=new Request.Builder()
.url("http://192.168.17.181:8080/TestWeb_war_exploded/login")
.post(body)
.build();
try (Response response=client.newCall(request).execute()){
result=response.body().string();
handler.sendEmptyMessage(0x111);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
第四步:Handler
public Handler handler=new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==0x111){
Toast.makeText(MainActivity2.this,result,Toast.LENGTH_SHORT).show();
}
}
};
4.OKHTTP的异步POST使用步骤
第一步:添加依赖
implementation("com.squareup.okhttp3:okhttp:4.10.0")
第二步:设置网络权限
在 AndroidManifest.xml 添加 Internet
<uses-permission android:name="android.permission.INTERNET" />
第三步:进行网络请求
1. 创建客户端
2. 构建请求体,将传递参数放到请求体中
3. 创建请求
4. 使用客户端发送请求
5. 创建回调方法
6. 处理返回结果
OkHttpClient okHttpClient=new OkHttpClient();
MediaType mediaType=MediaType.Companion.parse("application/json;charset=utf-8");
RequestBody body=RequestBody.Companion.create(param,mediaType);
Request request=new Request.Builder()
.url("http://192.168.17.181:8080/TestWeb_war_exploded/login")
.post(body)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
Log.i("aaa","bbb");
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
result=response.body().string();
handler.sendEmptyMessage(0x111);
}
});
第四步:Handler
public Handler handler=new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==0x111){
Toast.makeText(MainActivity2.this,result,Toast.LENGTH_SHORT).show();
}
}
};
5.同步和异步的小结
1. 同步需要开启线程,异步不需要开启线程
2. 同步和异步都需要跳转到 UI 线程修改 UI