依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
OkHttpClients 工具类
public class OkHttpClients {
private final OkHttpClient okHttpClient;
public OkHttpClients() {
//日志拦截器
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 OkHttpClients getInstance(){
return OkHttpHoulder.utils;
}
static class OkHttpHoulder{
private static final OkHttpClients utils=new OkHttpClients();
}
//封装同步的Get请求方式
public String getSync(String urlStr) throws IOException {
Request request = new Request.Builder().url(urlStr).build();
return okHttpClient.newCall(request).execute().body().string();
}
//同步的Post
public String postAync(String urlStr,String key,String value) throws IOException {
RequestBody body =new FormBody.Builder().add(key,value).build();
Request request = new Request.Builder().url(urlStr).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);
}
}
还有一种是MVP中直接使用OKHttp的写法
public class ModelImpl implements Model{
private MyCallBack myCallBack;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==0){
HomeBean bean = (HomeBean) msg.obj;
myCallBack.startSuccess(bean);
myCallBack.startError(bean);
}else{
MyBean myBean = (MyBean) msg.obj;
myCallBack.startSuccess(myBean);
myCallBack.startError(myBean);
}
}
};
@Override
public void start(final String urls, MyCallBack myCallBack) {
this.myCallBack = myCallBack;
if(urls=="http://www.wanandroid.com/tools/mockapi/6523/restaurants_offset_0_limit_4"){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(urls).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Reader reader = response.body().charStream();
HomeBean bean = new Gson().fromJson(reader, HomeBean.class);
handler.sendMessage(handler.obtainMessage(0,bean));
}
});
}else{
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(urls).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Reader reader = response.body().charStream();
MyBean bean = new Gson().fromJson(reader, MyBean.class);
handler.sendMessage(handler.obtainMessage(1,bean));
}
});
}
}
}