我们每次新做一个demo 或者新的项目时候 总会用到网络请求
不重复造轮子是程序员的美德
所以我们把请求的工具了你分装起来 复用岂不是更方便
我们可以新建一个NET 包
新建Api 接口
public interface Api {
//数据接口 String LOGIN_URL = "https://www.zhaoapi.cn/user/login";}
新建 MyIntercepter 实现Intercepter
import java.io.IOException; import okhttp3.FormBody; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; public class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Response response; //区分是GET请求还是POST请求 Request originRequest = chain.request(); if ("GET".equals(originRequest.method())) { HttpUrl httpUrl = originRequest.url() .newBuilder() .addQueryParameter("source", "android") .build(); Request request = new Request.Builder().url(httpUrl).build(); //发送请求 response = chain.proceed(request); } else { FormBody.Builder builder = new FormBody.Builder(); FormBody body = (FormBody) originRequest.body(); int size = body.size(); for (int i = 0; i < size; i++) { builder.add(body.name(i), body.value(i)); } builder.add("source", "android"); FormBody formBody = builder.build(); Request request = new Request.Builder() .url(originRequest.url()) .post(formBody) .build(); response = chain.proceed(request); } return response; } }
接下来就是重中之重了
OkhttpUntils 封装
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(new MyInterceptor()) .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); } }); } }); } }
OnNetListener 的接口
public interface OnNetListener { void onSuccess(String result); void onFailed(Exception e); }