这里主要是使用OkHttp封装 直接将接口和参数还有泛型以及请求类型直接传入就可以进行解析获取到类的实例!!
package com.bwie.qieziyisheng.utils;
import android.os.Handler;
import android.os.Looper;
import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
public class OkHttpUtils {
//声明
private static volatile OkHttpUtils okHttpUtils;
Handler handler;
private final Gson gson;
//声明接口
MCallBack mCallBack;
// 私有构造方法
private OkHttpUtils() {
handler = new Handler(Looper.getMainLooper());
gson = new Gson();
}
//设置方法
public static OkHttpUtils getOkHttpUtils() {
if (null == okHttpUtils) {
synchronized (OkHttpUtils.class) {
if (null == okHttpUtils) {
okHttpUtils = new OkHttpUtils();
}
}
}
return okHttpUtils;
}
//请求网络
public <T> void getMotheds(String url, final Class<T> cls) {
//得到Client对象
OkHttpClient okHttpClient = new OkHttpClient();
//创建Request
Request request = new Request.Builder().url(url).build();
//new Call
Call call = okHttpClient.newCall(request);
//接口回调
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
if (mCallBack != null) {
mCallBack.onFailure(request, e);
}
}
@Override
public void onResponse(Response response) throws IOException {
//设置解析
String json = response.body().string();
getJson(json, cls);
}
});
}
//设置拼接字符串的方法
public <T> void get(String url, Map<String, String> map, Class<T> cls) {
int i = 0;
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
//获取Value
String value = map.get(key);
//拼接字符串
if (i == 0) {
url += key + "=" + value;
i++;
} else {
url += "&" + key + "=" + value;
}
}
//解析
getMotheds(url, cls);
}
//解析
public <T> void getJson(String json, Class<T> cls) {
T t = gson.fromJson(json, cls);
mainThread(t);
}
//设置请求Post请求
public <T> void post(String url, Map<String,String> map, final Class<T> cls){
//得到Client对象
OkHttpClient okHttpClient=new OkHttpClient();
//得到Body
FormEncodingBuilder builder=new FormEncodingBuilder();
//遍历
Iterator<String> iterator=map.keySet().iterator();
while(iterator.hasNext()){
String key=iterator.next();
//获取Value
String value=map.get(key);
//加入到Builder里面
builder.add(key,value);
}
//得到Result
Request request=new Request.Builder().url(url).post(builder.build()).build();
//获取call
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
if(mCallBack!=null){
mCallBack.onFailure(request,e);
}
}
@Override
public void onResponse(Response response) throws IOException {
//获取Json
String json=response.body().string();
getJson(json,cls);
}
});
}
//将消息发送到主线程
public void mainThread(final Object result) {
handler.post(new Runnable() {
@Override
public void run() {
//接口回调
mCallBack.onResponse(result);
}
});
}
//设置接口
public interface MCallBack {
void onResponse(Object object);
void onFailure(Request request, IOException e);
}
//设置接口
public void setmCallBack(MCallBack callBack) {
this.mCallBack = callBack;
}
//请求网络
public <T> void initNetData(String url,Map<String,String> map,Class<T> cls,Methods methods){
switch (methods){
case GET:
get(url,map,cls);
break;
case POST:
post(url,map,cls);
break;
}
}
//生成枚举
public enum Methods {
GET,POST
}
}
OkHttp的封装及使用方法
最新推荐文章于 2021-09-12 08:26:43 发布