liteHttp初始化:
public class LiteHttpManager {//
protected static LiteHttp liteHttp;
public static LiteHttp getInstance() {
if (liteHttp == null) {
initLiteHttp(App.mContext);
}
return liteHttp;
}
private static void initLiteHttp(Context context) {
HttpConfig config = new HttpConfig(context) // configuration quickly
.setDebugged(true)// log output when debugged
.setDetectNetwork(true) // detect network before connect
.setDoStatistics(true)
.setDefaultMaxRetryTimes(3)
.setDefaultCharSet("UTF-8")
.setDebugged(Utils.isDebug())
.setForRetry(3, false) // statistics of time and traffic
//.setDisableNetworkFlags(HttpConfig.FLAG_NET_DISABLE_MOBILE) //不用2/3/4G网络
.setUserAgent("Mozilla/5.0 (...)") // set custom User-Agent
.setTimeOut(8000, 50000); // connect and socket timeout: 2s
liteHttp = LiteHttp.newApacheHttpClient(config);
}
}
liteHttp 请求网络:
public void requestInspectionPoolData(String time, String inspectiontype) {
StringRequest postRequest = new StringRequest("url");
LinkedList<NameValuePair> pList = new LinkedList<NameValuePair>();
pList.add(new NameValuePair("User", "");
pList.add(new NameValuePair("possword", "");
postRequest.setHttpBody(new UrlEncodedFormBody(pList));
postRequest.setMethod(HttpMethods.Post);
postRequest.setHttpListener(new HttpListener<String>() {
@Override
public void onSuccess(String s, Response<String> response) {
super.onSuccess(s, response);
Log.i(TAG, "onSuccess: " + s);
//解析服务器返回数据
}
@Override
public void onFailure(HttpException e, Response<String> response) {
super.onFailure(e, response);
//网络请求失败
}
});
//异步访问
LiteHttpManager.getInstance().executeAsync(postRequest);
}
liteHttp 封装 HTTPBody:
ApiParameter:
/**
* Created by Alen on 2017/2/11.
* <p>
* 请求接口的父类
*/
public class ApiParameter {
// 子类 如果要添加参数需要重写该方法, 否则默认为无 额外参数
//
public ApiParamMap buildExterParameter() {
return new ApiParamMap();
}
// 子类, 如果需要添加sessionKey 需要覆盖此函数并且返回true
public boolean needSessionKey() {
return false;
}
public MultipartBody getRequestBody() {
ApiParamMap apiParamMap = buildExterParameter();
apiParamMap.put("os", new ApiParamMap.ParamData("android"));
apiParamMap.put("appVersion", new ApiParamMap.ParamData(Utils.getVersion()));
apiParamMap.put("versionCode", new ApiParamMap.ParamData(Utils.getVersionCode() + ""));
apiParamMap.put("channel", new ApiParamMap.ParamData(Utils.getChannelName()));
apiParamMap.put("osVersion", new ApiParamMap.ParamData(android.os.Build.VERSION.SDK_INT + ""));
MultipartBody body = new MultipartBody();
List<String> lists = new ArrayList<String>(apiParamMap.keySet());
for (String key : lists) {
if(apiParamMap.get(key).value == null) apiParamMap.get(key).value = "";//防止外面过来空对象 导致崩溃
body.addPart(new StringPart(key, apiParamMap.get(key).value));
}
return body;
}
}
ApiParamMap:
public class ApiParamMap extends LinkedHashMap<String, ApiParamMap.ParamData> {
public ApiParamMap() {
super();
}
public static class ParamData {
public String value;
public ParamData(String value) {
this.value = value;
}
}
}
ApiParameter 子类:
public class ActionParmeter extends ApiParameter {
private String user;
private String password;
//方法参数为调用URL需要的参数
public ActionParmeter(String user, String password) {
this.user= user;
this.password= password;
}
//重写父类方法
@Override
public ApiParamMap buildExterParameter() {
ApiParamMap map=new ApiParamMap();
map.put("user", new ApiParamMap.ParamData(this.user));
map.put("password", new ApiParamMap.ParamData(this.password));
return map;
}
}
调用:
stringRequest.setHttpBody(new ActionDetailsParmeter(user,password).getRequestBody());