大家好,又见面了,我是你们的朋友全栈君。
public class MainActivity extends AppCompatActivity {
String path="https://www.zhaoapi.cn/ad/getAd";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void getTong(View view) {
new Thread(){
@Override
public void run() {
super.run();
OkHttpClient okHttpClient = new OkHttpClient();//创建客户端对象 Request builder = new Request.Builder()
.url(path)//指定访问路径 .build();//提交请求 Call call = okHttpClient.newCall(builder);//用客户端调用请求对象 //开始执行 ....指定是同步还是异步 Response execute = null;//同步 try {
execute = call.execute();
if(execute.isSuccessful()){
//判断是否请求成功 final String pp=execute.body().string();
// Log.i("jiba","------"+pp); runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,pp.substring(0,20),Toast.LENGTH_SHORT).show();
}
});
}else{
Log.i("jiba","你是sss!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void getYi(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
Request builder = new Request.Builder().url(path).build();
Call call = okHttpClient.newCall(builder);
call.enqueue(new Callback() {
//请求错误所走的方法 @Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();//打印错误日志 }
//请求成功所走的方法 @Override
public void onResponse(Call call, Response response) throws IOException {
String s = response.body().string();
Log.i("jiba","======"+s);
}
});
}
public void postTong(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
FormBody formBody = new FormBody.Builder()
.add("mobile","18360981994")
.add("password","123456")
.build();
Request builder = new Request.Builder()
.post(formBody)
.url("https://www.zhaoapi.cn/user/reg")
.build();
Call call = okHttpClient.newCall(builder);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, string,Toast.LENGTH_SHORT).show();
}
});
}
});
}
public void postShangchuan(View view) {
//A.检查用户是否已经允许了权限....PackageManager.PERMISSION_GRANTED代表的是用户已经允许 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//B.不允许...的时候,,,请求用户允许这个权限 // Activity arg0代表当前的activity, @NonNull String[] arg1请求的权限的数组,也就是需要请求允许哪些权限, int arg2请求码 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1001);
}else {
//允许...上传文件 postFile();
}
}
private void postFile() {
OkHttpClient okHttpClient = new OkHttpClient();
//2.指定文件的类型 image/jpg image/png video/mp4 ...mimeType MediaType mediaType = MediaType.parse("text/x-markdown;charset=utf-8");
//3.指定要上传的文件对象 File file = new File(Environment.getExternalStorageDirectory(),"wang.txt");
Request request = new Request.Builder()
//上传文件的时候请求体使用RequestBody.create()获取okhttp3.MediaType contentType // 文件的类型,@NotNull java.io.File file上传的文件对象 .post(RequestBody.create(mediaType, file))
.url("https://api.github.com/markdown/raw")
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(MainActivity.this,response.body().string(),Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context="one.bw.com.okhttpqingqiu.MainActivity">
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="okhttp的get同步" android:onClick="getTong" />
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="okhttp的get异步" android:onClick="getYi" />
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="okhttp的post异步" android:onClick="postTong" />
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="post上传文件" android:onClick="postShangchuan" />
</LinearLayout>
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127786.html原文链接:https://javaforall.cn