okHttp 封装

private static OkHttpUtils okHttpUtils = null;
    	private MyHandler myHandler = new MyHandler();
    	private OkLoadListener okLoadListener;
    
    	//单例
    	public static OkHttpUtils getInstance() {
    		if (okHttpUtils == null) {
    			okHttpUtils = new OkHttpUtils();
    		}
    		return okHttpUtils;
    	}
    
    	//get
    	public void okGet(String url) {
    		OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new MyInter()).build();
    		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 message = myHandler.obtainMessage();
    				message.what = 0;
    				message.obj = e.getMessage();
    				myHandler.sendMessage(message);
    			}
    
    			@Override
    			public void onResponse(Call call, Response response) throws IOException {
    				Message message = myHandler.obtainMessage();
    				message.what = 1;
    				message.obj = response.body().string();
    				myHandler.sendMessage(message);
    			}
    		});
    	}
    
    	//post
    	public void okPost(String url, Map<String, String> map) {
    		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) {
    			String value = map.get(key);
    			builder.add(key, value+"");
    		}
    		//build
    		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 message = myHandler.obtainMessage();
    				message.what = 0;
    				message.obj = e.getMessage();
    				myHandler.sendMessage(message);
    			}
    
    			@Override
    			public void onResponse(Call call, Response response) throws IOException {
    				Message message = myHandler.obtainMessage();
    				message.what = 1;
    				message.obj = response.body().string();
    				myHandler.sendMessage(message);
    			}
    		});
    	}
    
    	//拦截器
    	class MyInter implements Interceptor {
    		private static final String TAG = "MyInter";
    		@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;
    		}
    	}
    
    	//handler
    	class MyHandler extends Handler {
    		@Override
    		public void handleMessage(Message msg) {
    			switch (msg.what) {
    				case 0:
    					//失败
    					String e = (String) msg.obj;
    					okLoadListener.okLoadError(e);
    					break;
    				case 1:
    					//成功
    					String json = (String) msg.obj;
    					okLoadListener.okLoadSuccess(json);
    					break;
    			}
    		}
    	}
    
    	//提高外部调用的接口
    	public void setOkLoadListener(OkLoadListener okLoadListener) {
    		this.okLoadListener = okLoadListener;
    	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值