根据字面意思胡写,方便作者自己记忆。。。
依赖:
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
package com.example.rikao_13.Until;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OKHttpUntil2 {
private Handler mHandler=new Handler(Looper.getMainLooper());
/**
* 1.单例
*/
private static OKHttpUntil2 instance;
private final OkHttpClient mClient;
private HttpLoggingInterceptor mInterceptor;
public static OKHttpUntil2 getInstance(){
if (instance==null){
synchronized (OKHttpUntil2.class){
if (null==instance){
instance=new OKHttpUntil2();
}
}
}
return instance;
}
//日志拦截器
mInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("TAG","message="+message);
}
});
mInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
/**
* 2.构造方法
*/
mClient = new OkHttpClient.Builder()
.connectTimeout(10,TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.readTimeout(10,TimeUnit.SECONDS)
.addInterceptor(mInterceptor)
.build();
}
/**四个方法:一
* get同步:根据需求建立请求---->客户端开启命令---->返回得到执行后的结果
*/
public String getExcute(String path) throws IOException {
//1.创建请求
Request builder = new Request.Builder()
.get().url(path).build();
//2.客户端新建一个关于请求的命令
Call call = mClient.newCall(builder);
//命令执行后的结果
Response response = call.execute();
//从结果中得到数据
String string = response.body().string();
return string;
}
/**
* 二,get异步:
* 根据需求开启一个请求---->客户端开启新的命令---->命令失败/成功
*/
public void getEnquenue(String path, final MyCallBack callBack, final Class clazz){
Request request = new Request.Builder().get().url(path).build();
Call call = mClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(final Call call, final IOException e) {
mHandler.post(new Runnable() {
@Override
public void run() {
callBack.fail(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(string, clazz);
mHandler.post(new Runnable() {
@Override
public void run() {
callBack.success(o);
}
});
}
});
}
/**
* 异步post:
* 将需求放入表单---->建立请求体---->开启一个请求---->客户端开启新的关于请求的命令---->命令失败/成功
*/
public void postEnquenue(String path, Map<String,String>map, final Class clazz, final MyCallBack callBack){
//1.创建表单formbody
FormBody.Builder body = new FormBody.Builder();
//2.把取出的key和values放入
for (Map.Entry<String,String>entry:map.entrySet()) {
body.add(entry.getKey(),entry.getValue());
}
RequestBody build = body.build();
//异步post、同步get
Request request = new Request.Builder().post(build).url(path).build();
//得到request请求,客户端就开启一个关于请求的命令
Call call = mClient.newCall(request);
//异步请求要在handler中执行
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
mHandler.post(new Runnable() {
@Override
public void run() {
callBack.fail(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//添加try catch,防止解析崩溃!!!!!!!!!
try{
//成功,得到数据
String string = response.body().string();
Gson gson = new Gson();
//进行解析后,得到想要的类型数据
final Object o = gson.fromJson(string, clazz);
mHandler.post(new Runnable() {
@Override
public void run() {
callBack.success(o);
}
});
}catch(Exception e){
callBack.fail(e);
}
}
});
}
}