OKHttp异步get,post请求和同步请求
来源:互联网 发布:淘宝发货是谁发货 编辑:程序博客网 时间:2024/05/17 06:43
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// postEnqueue();
}
//
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());
}
});
}
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());
}
});
}
private void execute(){
//同步方法
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts?pscid=39&page=1").build();
Call call = client.newCall(request);
Response response = call.execute() ;
String result = response.body().string();
System.out.println("result = " + result);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// postEnqueue();
}
//
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());
}
});
}
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());
}
});
}
private void execute(){
//同步方法
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts?pscid=39&page=1").build();
Call call = client.newCall(request);
Response response = call.execute() ;
String result = response.body().string();
System.out.println("result = " + result);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
阅读全文
0 0
- OKHttp异步get,post请求和同步请求
- okhttp 第三方 网络数据请求 异步get请求 和异步post请求 同步请求
- OkHttp的get/post同步异步请求
- Android OKHttp同步get请求,同步post请求,异步get请求,异步post请求
- Okhttp 异步get post 请求
- OKhttp:Get同步请求丶Get异步请求丶Post异步请求丶上传文件
- OKhttp:Get同步请求丶Get异步请求丶Post异步请求丶上传文件
- Get请求和Post请求 同步下载和异步下载
- OkHttp get请求 和post请求
- OKhttp的get请求和post请求
- OkHttp学习(1)-->>同步和异步(get、post键值对、post带map、请求头体封装json)
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- TensorFlow发布「eager」模式,更易操作!
- 圆形进度条
- Java使用keytool工具导出RSA密钥证书及导出公钥文件
- solr cloud重新设置schema
- HttpGet
- OKHttp异步get,post请求和同步请求
- 冈萨雷斯数字图像处理学习5:图像复原和重建1 噪声单独影响
- UltraEdit出现“初始化FTP失败”解决记录
- 新手请看完整版web服务器的架设
- Alloyteam Conf 2017 前端大会
- 技术文章
- TCP/IP数据包结构详解
- Android 线程通信(Handler + Message + Looper) 4
- HttpPost