写一个ok的接口
public interface OnNetListener {
void onSuccess(String result);void onFailed(Exception e);
}
让ok类实现ok接口
import android.os.Handler;import android.os.Looper;
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.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OkhttpUtils {
private static OkhttpUtils instance;
private final OkHttpClient okHttpClient;
private final Handler handler;
private OkhttpUtils() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)//连接超时
.writeTimeout(20, TimeUnit.SECONDS)//写入超时
.readTimeout(20, TimeUnit.SECONDS)//读取超时
.addInterceptor(logging)
.build();
handler = new Handler(Looper.getMainLooper());
}
public static OkhttpUtils getInstance() {
if (instance == null) {
instance = new OkhttpUtils();
}
return instance;
}
/**
* GET请求
*
* @param url
*/
public void doGet(String url, final OnNetListener onNetListener) {
//创建Request
final Request request = new Request.Builder()
.url(url)
.build();
//发送请求
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//拿到服务器返回的数据
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(string);
}
});
}
});
}
/**
* POST请求
*
* @param url
* @param params
* @param onNetListener
*/
public void doPost(String url, Map<String, String> params, final OnNetListener onNetListener) {
FormBody.Builder builder = new FormBody.Builder();
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
//创建FormBody
FormBody formBody = builder.build();
//创建Request
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
//请求数据
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//拿到服务器返回的数据
final String string = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
onNetListener.onSuccess(string);
}
});
}
});
}
}