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());
}
});