布局文件
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dash.a02_okhttp.MainActivity">
<Button
android:text="get同步"
android:onClick="getTongBu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="get异步"
android:onClick="getYiBu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="post异步"
android:onClick="postYiBu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="post上传"
android:onClick="postShangChuan"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
get同步异步请求
public void getTongBu(View view) {
new Thread(){
@Override
public void run() {
//1.创建一个okhttp客户端对象
OkHttpClient okHttpClient = new OkHttpClient();
//2.创建一个请求的对象....默认就是get方式
Request request = new Request.Builder()
.url("https://www.zhaoapi.cn/ad/getAd")
.build();
//3.客户端要去调用一个请求的对象
Call call = okHttpClient.newCall(request);
//4.执行....指定同步还是异步请求的方式....call.execute()同步的方式
try {
Response response = call.execute();
if (response.isSuccessful()){
/**
* * 响应体的 string() 方法对于小文档来说十分方便、高效。但是如果响应体太大(超过1MB),
* 应避免适应 string()方法 ,因为他会将把整个文档加载到内存中。
* 对于超过1MB的响应body,应使用流的方式来处理body。
*/
Log.i("------",response.body().string());
//设置适配器....handler...runOnUiThread()
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void getYiBu(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url("https://www.zhaoapi.cn/ad/getAd")
.build();
Call call = okHttpClient.newCall(request);
//指定call调用的方式
call.enqueue(new Callback() {
//失败
@Override
public void onFailure(Call call, IOException e) {
//打印异常的日志
e.printStackTrace();
}
//服务器有响应....这个位置仍然是工作线程
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
//response.body().string();
//java.lang.IllegalStateException: closed 非法状态异常:关闭...本次请求已经响应,,,关闭
Log.i("------",response.body().string());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"请求成功",Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
post异步请求
/**
* post和get都有同步和异步的方式.....区别就在于call调用的方法不同
* @param view
*/
public void postYiBu(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
//2.传递参数使用FormBody请求实体对象
FormBody formBody = new FormBody.Builder()
.add("mobile", "15715317583")
.add("password", "123456")
.build();
//3.获取post方式的请求对象
Request request = new Request.Builder()
.post(formBody)
.url("https://www.zhaoapi.cn/user/reg")
.build();
//4.
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (response.isSuccessful()){
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Toast.makeText(MainActivity.this,response.body().string(),Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
});
}