get、post同步异步请求---Log日志打印
程序员文章站
2022-04-15 12:11:51
...
package wanghuiqi.bawie.com.get_asyn;
import android.app.DownloadManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.textclassifier.TextClassification;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private Button getAsyn;
private Button postAsyn;
private Button getAyn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get异步请求
getAsyn = findViewById(R.id.get_asyn);
postAsyn = findViewById(R.id.post_asyn);
getAyn = findViewById(R.id.get_syn);
get异步
//get异步
getAsyn.setOnClickListener(new View.OnClickListener() {
private Request request;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onClick(View v) {
OkHttpClient client = new OkHttpClient();
//创建一个request对象
request = new Request.Builder()
.url("http://120.27.23.105/product/getProducts?pscid=39&page=1")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
}
@Override
public void onResponse(okhttp3.Call call, final Response response) throws IOException {
System.out.println("getEnqueue = "+response.body().string());
}
});
}
});
post异步
//post异步请求
postAsyn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OkHttpClient client = new OkHttpClient();
//post方式 需要表单提交参数
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("postEnqueue = "+response.body().string());
}
});
}
});
get同步
getAyn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//同步方法.需要开启子线程里运行
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("getExecute = "+result);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'