OkHttpClient的依赖
implementation 'com.squareup.okhttp3:okhttp-ws:3.4.2'
日志拦截器的依赖
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
//同步处理get方式 必须放在子线程里
private void sendNet() {
new Thread(new Runnable() {
@Override
public void run() {
try {
//1:学什么都能new出一个对象来
OkHttpClient okHttpClient = new OkHttpClient();
//Request就是请求的类
Request request = new Request.Builder().url(mUrl).build();
//发送请求newCall方法
Call call = okHttpClient.newCall(request);
//通过call去处理给你响应Response
Response response = call.execute();
//从相应体里面拿到数据
String string = response.body().string();
Log.e("string", string);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//同步处理POST方式
private void sendPost() {
new Thread(new Runnable() {
@Override
public void run() {
try {
//1:学什么都能new出一个对象来
OkHttpClient okHttpClient = new OkHttpClient();
//创建请求体
RequestBody requestBody = new FormBody.Builder()
.add("mobile", "18513426687")//键值对的形式
.build();
//Request就是请求的类
Request request = new Request.Builder().url(mPostUrl).post(requestBody).build();
//发送请求newCall方法
Call call = okHttpClient.newCall(request);
//通过call去处理给你响应Response
Response response = call.execute();
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//异步的Get方式
private void asyncSend() {
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder().url(mUrl).build();
Call call = okHttpClient.newCall(request);
//异步处理
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("onFailure", "onFailure");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
// Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
//一个小知识 不建议使用 在子线程直接刷新UI操作
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
}
});
}
});
}
//异步POST
private void asyncPost() {
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("mobile", "18513426687")
.add("password", "123456").build();
//链式调用
Request request = new Request.Builder().url(mPostUrl).post(body).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("onFailure", "onFailure");
}
@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();
}
});
}
});
}
OkHttpClient的封装类,可以将这些全部封装起来
public class OkUtils {
private OkHttpClient okHttpClient;
private OkUtils() {
//日志拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)//响应时间,读取时间
.readTimeout(20, TimeUnit.SECONDS)
.callTimeout(20, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)//添加日志拦截器
.build();
}
//静态内部类单例模式
public static OkUtils getInstance() {
return OkHolder.okUtils;
}
static class OkHolder {
private static final OkUtils okUtils = new OkUtils();
}
//这是同步的get和post
public String getSync(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
return okHttpClient.newCall(request).execute().body().string();
}
public String postSync(String url, String key, String value) throws IOException {
RequestBody body = new FormBody.Builder().add(key, value).build();
Request request = new Request.Builder().url(url).post(body).build();
return okHttpClient.newCall(request).execute().body().string();
}
//异步的get和post
public void getAsync(String url, Callback callback) {
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
public void postAsync(String url, Callback callback) {
RequestBody body = new FormBody.Builder().add("key", "value").build();
Request request = new Request.Builder().url(url).post(body).build();
okHttpClient.newCall(request).enqueue(callback);
}
}