首先添加依赖
Utils类:
import android.os.Handler; import android.os.Message; import java.io.IOException; import java.util.Map; import java.util.Set; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class OkHttpUtils { private MyHandler myHandler=new MyHandler(); private static OkHttpClient okHttpClient=null; private OkInterface okInterface; //单列模式 public static OkHttpClient getInstance(){ if (okHttpClient==null){ okHttpClient=new OkHttpClient(); } return okHttpClient; } //OkHttp get请求 public void okGet(String url){ //实例化OkHttpClient OkHttpClient client = new OkHttpClient(); //创建请求对象 Request request = new Request.Builder().url(url).build(); //创建请求队列 Call call = client.newCall(request); //开始请求 call.enqueue(new Callback() { //失败 @Override public void onFailure(Call call, IOException e) { Message msg = myHandler.obtainMessage(); msg.what=0; msg.obj="失败"; myHandler.sendMessage(msg); } //成功 @Override public void onResponse(Call call, Response response) throws IOException { Message msg = myHandler.obtainMessage(); msg.what=1; msg.obj=response.body().string(); myHandler.sendMessage(msg); } }); } //OkHttp post请求 public void okPost(String url,Map<String,String> map){ //实例化OkHttpClient OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyInter()).build(); //创建FormBody FormBody.Builder builder = new FormBody.Builder(); //遍历map Set<String> keys = map.keySet(); for (String key:keys) { //取出Value值 String value = map.get(key); //添加到FormBody中 builder.add(key,value); } //闭合FormBody FormBody body = builder.build(); //创建请求对象 Request request = new Request.Builder().url(url).post(body).build(); //创建请求队列 Call call = client.newCall(request); //开始请求 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Message msg = myHandler.obtainMessage(); msg.what=0; msg.obj="失败"; myHandler.sendMessage(msg); } @Override public void onResponse(Call call, Response response) throws IOException { Message msg = myHandler.obtainMessage(); msg.what=1; msg.obj=response.body().string(); myHandler.sendMessage(msg); } }); } //创建Handler class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0://失败 //获取传递过来的值 String error = (String) msg.obj; //调用接口的方法 okInterface.error(error); break; case 1://成功 //获取传递过来的值 String json = (String) msg.obj; //调用接口的方法 okInterface.success(json); break; } } } //创建拦截器 class MyInter implements Interceptor{ @Override public Response intercept(Chain chain) throws IOException { //获取原来的body Request request = chain.request(); RequestBody body = request.body(); if (body instanceof FormBody) { //遍历原来的所有参数,加到新的Body里面,最后将公共参数加到新的Body FormBody.Builder newBuilder = new FormBody.Builder(); for (int i = 0; i < ((FormBody) body).size(); i++) { String name = ((FormBody) body).name(i); String value = ((FormBody) body).value(i); //放入新的 newBuilder.add(name, value); } //在将公共参数添加进去 newBuilder.add("source", "android"); FormBody newBody = newBuilder.build(); //创建新的请求 Request newRequest = request.newBuilder().post(newBody).build(); Response response = chain.proceed(newRequest); return response; } return null; } } //创建接口 public interface OkInterface{ //请求失败的方法 void error(String error); //请求成功的方法 void success(String json); } //提供外部访问的方法 public void getOkHttp(OkInterface okInterface){ this.okInterface=okInterface; } }MainActivity类:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.example.okhttp.utils.Utils; public class MainActivity extends AppCompatActivity { String uriString="http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //Button的点击事件 public void bu(View v){ //实例化 Utils utils = new Utils(); //调用解析json的方法 utils.okhttp(uriString); //调用接口的外部方法 utils.getokhttp(new Utils.HttpGet() { //重写的失败的方法 @Override public void error(String error) { Toast.makeText(MainActivity.this,error,Toast.LENGTH_SHORT).show(); } //重写成功的方法 @Override public void success(String json) { Toast.makeText(MainActivity.this,json,Toast.LENGTH_SHORT).show(); } }); } }