通常我们会引用许多lib
并且会出现lib 与我们的功能只差一点点 这样的情况我们最好不要去修改源码 而是进行二次封装
举例我使用 xUtils的二次封装 此处说明我是搞ios的 这个是android 今天mac 机子没网 蛋疼
使用到pulltorefresh 和 xUtils
- 网络封装
由于此处是没有文件处理所以我把参数转换里面file给去掉了 如果需要自行加上即可
public class ApiHttpUtil {
final public static String base_url_api = Constants.base_url_api;
private String api_url;
HttpApiCallBack callBack;
Map<String, String> paramsMap;
CacheEntity cacheEntity;
Long timeOut = 0L;
public ApiHttpUtil() {
}
public ApiHttpUtil(HttpApiCallBack callBack) {
this.callBack = callBack;
this.cacheEntity = new CacheEntity();
}
public ApiHttpUtil(String url, HttpApiCallBack callBack) {
this.api_url = url;
this.callBack = callBack;
this.cacheEntity = new CacheEntity();
}
public String getApiUrl() {
if (StringUtils.isEmpty(api_url)) {
return base_url_api + "?";
}
if (api_url.startsWith("http")) {
return api_url;
}
return base_url_api + "/" + api_url;
}
public String strValue(Object obj) {
if (obj instanceof String) {
return (String) obj;
} else if (obj instanceof Integer) {
return Integer.toString((Integer) obj);
} else if (obj instanceof Double) {
return Double.toString((Double) obj);
} else {
// TODO 其他 object 类转换 http string
}
return String.valueOf(obj);
}
public void getObjs(Object... objs) {
Map<String, Object> map = getMap(objs);
get(map);
}
public void postObjs(Object... objs) {
Map<String, Object> map = getMap(objs);
post(map);
}
/*
* key --- String Integer Double value -- +File
*/
public Map<String, Object> getMap(Object[] objs) {
Map<String, Object> map = new HashMap<String, Object>();
if (objs.length % 2 == 0) {
for (int i = 0; i < objs.length; i += 2) {
try {
String key = (String) strValue(objs[i]);
Object value = null;
if (objs.length > i + 1) {
Object objs2 = objs[i + 1];
value = strValue(objs2);
}
if (value instanceof String) {
if (!StringUtils.isEmpty((String) value)
&& !StringUtils.isEmpty(key)) {
map.put(key, value);
}
} else if (value instanceof File) {
map.put(key, value);
}
} catch (Exception e) {
}
}
}
return map;
}
public void get() {
get(null);
}
public void post() {
post(null);
}
public ApiHttpUtil setTimeOut(int second) {
this.timeOut = second * 1000L;
return this;
}
public void post(Map<String, Object> map) {
RequestParams params = new RequestParams();
if (map != null) {
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof String) {
params.addBodyParameter(entry.getKey(),
(String) entry.getValue());
} else {
params.addBodyParameter(entry.getKey(),
strValue(entry.getValue()));
}
}
}
if (timeOut <= 0 && !useCache(map)) {
send(HttpRequest.HttpMethod.POST, params);
}
}
public void get(Map<String, Object> map) {
RequestParams params = new RequestParams();
if (map != null) {
for (Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof String) {
params.addQueryStringParameter(entry.getKey(),
(String) entry.getValue());
} else {
params.addQueryStringParameter(entry.getKey(),
strValue(entry.getValue()));
}
}
}
if (timeOut <= 0 && !useCache(map)) {
send(HttpRequest.HttpMethod.GET, params);
}
}
public boolean useCache(Map<String, Object> map) {
this.cacheEntity.setKey(getApiUrl() + map.toString());
if (AppContext.mInstance != null) {
if (!StringUtils.isEmpty(this.cacheEntity.getValue())) {
if (!AppUtils.isNetworkAvailable(AppContext.mInstance)) {
AppUtils.openNetworkSettings(AppContext.mInstance);
onSuccess(this.cacheEntity.getCacheValue());
return true;
}
}
String result = this.cacheEntity.getValue(timeOut);
if (!StringUtils.isEmpty(result)) {
onSuccess(result);
return true;
}
}
return false;
}
public void send(HttpRequest.HttpMethod method, RequestParams params) {
HttpUtils http = new HttpUtils();
http.send(method, getApiUrl(), params,
new RequestCallBack<String>() {
@Override
public void onStart() {
callBack.onStart();
}
@Override
public void onLoading(long total, long current,
boolean isUploading) {
callBack.onLoading(total, current, isUploading);
}
@Override
public void onCancelled() {
callBack.onCancelled();
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
ApiHttpUtil.this.onSuccess(responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
callBack.onFailure(error, msg);
}
});
}
public boolean