get請求
使用xUtils的HttpUtlis進行網絡請求有GET/POST兩種方式:
當需要帶參數請求的時候要注意:
GET 請求參數:
RequestParams params = new RequestParams();
params.addQueryStringParameter(“key”, value);
public NetParams(String url, int timeOut, Map<String, Object> parameters) {
super(ConstBuild.SERVICE_NAME + url);
setConnectTimeout(timeOut == 0 ? 30 * 1000 : timeOut * 1000);
addQueryStringParameter("clientOs", "1");
long timestamp = System.currentTimeMillis() / 1000;
Set<Map.Entry<String, Object>> set = parameters.entrySet();
for (Map.Entry<String, Object> entry : set) {
this.addQueryStringParameter(entry.getKey(), entry.getValue());
}
}
post請求參數
RequestParams params = new RequestParams();
params.addBodyParameter(“key”, value);
/**
* 發起網絡請求的訪問參數設置
*
* @param url 方法
* @param parameters 參數
* @param timeOut 訪問超時(毫秒)
*/
public static RequestParams getNetBodyParams(String url, Map parameters, int timeOut) {
RequestParams p = new RequestParams(ConstBuild.SERVICE_NAME + url);
p.setConnectTimeout(timeOut == 0 ? 30 * 1000 : timeOut);
p.setReadTimeout(2 * 60 * 1000);
p.setAutoResume(false);
p.setMaxRetryCount(1);
p.setCancelFast(true);
p.setReadTimeout(120 * 1000);
p.addQueryStringParameter("clientOs", "1");
Set<Map.Entry<String, Object>> set = parameters.entrySet();
for (Map.Entry<String, Object> entry : set) {
p.addBodyParameter(entry.getKey(), entry.getValue());
}
return p;
}
注意:如果使用post請求時,使用了get的addQueryString方法,則在參數過長時,會報以下錯誤:
netexception,socket exception,413錯誤,