OkHttp使用教程
来源:互联网 发布:quiver linux 编辑:程序博客网 时间:2024/06/13 05:25
get请求
private void enqueue(){
// 创建发送求的client 对象
OkHttpClient client = new OkHttpClient();
// 创建一个request 对象 ,request 对象 构造包含 请求方法 (get post put delete),请求接口地址
Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts?pscid=39&page=1").build();
// newCall 返回Call(实际上 RealCall ) 对象,实际底层 把网络请求放入了一个请求队列
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败回调
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// 成功回调
// System.out.println("call = " + Thread.currentThread().getName());
System.out.println("response = " + response.body().string());
}
});
}
post请求
private void postEnqueue(){
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.connectTimeout(10,TimeUnit.SECONDS)
.build();
// FormBody 表单
RequestBody requestBody = new FormBody.Builder()
.add("pscid","39")
.add("page","1")
.build();
Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "1111", Toast.LENGTH_SHORT).show();
}
});
System.out.println("response = " + response.body().string());
}
});
- OkHttp使用教程
- OKHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp的使用教程
- OkHttp使用教程
- OkHttp使用教程
- OkHttp使用教程
- OKhttp使用教程
- OkHttp使用教程
- Newtonsoft.Json简要使用笔记
- 7.1 键盘获取3个整型数据
- 交流和学习2
- python 类class
- Centos7配置阿里yum源
- OkHttp使用教程
- ShellExecute(打开文件所在文件夹并定位到文件)
- 五分钟搞懂并查集
- 杭电oj1004
- 唯快不破:从TCP协议的原理来谈谈rst复位攻击
- java服务端知识点总结
- Hive 以及mysql 中如何做except 数据操作
- java中的static关键字
- RabbitMQ 概念说明