android http2.0请求,Android http HttpURLConnection
/**
*?Http?get?請(qǐng)求
*
*?@param?urlPath
*
*
*/
private?static?String?httpConnByGet(RequestUrl?ru,Params?params){
String?token?=?(String)?params.getMap().get("token")?;
String?et?=?(String)?params.getMap().get("et")?;
params.getMap().remove("et");
if(token?!=?null?){
params.getMap().put("token",Md5.md5(token?+?et?+?params.getMap().get("uid")?+?ru.getKeywords()));
}
String?urlPath?=?ru.getUrl()?+?HttpURLConstant.QMARK?+?HttpURLConstant.COMMON_PARAMS?+?et?+?params.toString()?;?//通用參數(shù)的設(shè)置
Log.w("NETURL","geturl:"+urlPath);
URL?url?=?null;
HttpURLConnection?urlConnection?=?null;
try?{
ByteArrayOutputStream?outStream?=?new?ByteArrayOutputStream();
byte[]?data?=?new?byte[2048];????//應(yīng)該不會(huì)小吧!
int?len?=?0;
url?=?new?URL(urlPath);
urlConnection?=?(HttpURLConnection)?url.openConnection();
//???Class>?clazz=url.openConnection().getClass();??//看看返回什么鬼類(lèi)型
//???Class>?clazzSuper=clazz.getSuperclass();???????//父類(lèi)又是什么鬼啊!
//getInputStream暗中執(zhí)行了連接,不需要urlConnection.connect();
urlConnection.setReadTimeout(readTimeout);??????????????????????????????????????????????????//?設(shè)置請(qǐng)求的超時(shí)時(shí)間
urlConnection.setConnectTimeout(connectTimeout);
if?(urlConnection.getResponseCode()?==?HttpURLConnection.HTTP_OK)?{
InputStream?inStream?=?urlConnection.getInputStream();???//獲取輸入流,此時(shí)才真正建立鏈接
while?((len?=?inStream.read(data))?!=?-1)?{
outStream.write(data,?0,?len);
}
inStream.close();
if(isDebug){Log.e(TAG,"get?返回?cái)?shù)據(jù)"+new?String(outStream.toByteArray()));}
return?new?String(outStream.toByteArray());
}else{?//返回的是HTTP的錯(cuò)誤碼,不是我們自己定義的錯(cuò)誤碼!
//處理一下http的錯(cuò)誤返回碼
Log.e(TAG,"get?網(wǎng)絡(luò)連接失敗"+urlConnection.getResponseCode());
return?"HTTP_ERROR-"+urlConnection.getResponseCode();
}
}?catch?(MalformedURLException?e)?{
e.printStackTrace();
Log.e(TAG,"MalformedURLException?e");
}?catch?(IOException?e)?{
e.printStackTrace();
Log.e(TAG,"IOException?e");
}?finally{
if(urlConnection!=null){
urlConnection.disconnect();??//總是需要關(guān)閉的吧!
}
}
return?null;
}
/**
*?Http?Post?請(qǐng)求
*
*?@param?urlpath
*?@param?paramsDatas
*/
private?static?String?httpConnByPost(RequestUrl?ru,Params?params)?{
String?token?=?(String)?params.getMap().get("token")?;
String?et?=?(String)?params.getMap().get("et")?;
params.getMap().remove("et");
if(token?!=?null?){
params.getMap().put("token",?Md5.md5(token?+?et?+?params.getMap().get("uid")?+?ru.getKeywords()));
}
URL?url?=?null;???????????????????????????????????????????????????????//?根據(jù)地址創(chuàng)建URL對(duì)象
HttpURLConnection?urlConnection?=?null;???????????????????????????????//?根據(jù)URL對(duì)象打開(kāi)鏈接
try?{
url?=?new?URL(ru.getUrl());??????????????????????????????????????????????????????????//?根據(jù)地址創(chuàng)建URL對(duì)象
urlConnection?=?(HttpURLConnection)?url.openConnection();????????????????????????????//?根據(jù)URL對(duì)象打開(kāi)鏈接
urlConnection.setRequestMethod("POST");???????????????????????????????????????????????//?設(shè)置請(qǐng)求的方式
urlConnection.setReadTimeout(readTimeout);????????????????????????????????????????????//?設(shè)置請(qǐng)求的超時(shí)時(shí)間
urlConnection.setConnectTimeout(connectTimeout);
String?paramsData?=?HttpURLConstant.COMMON_PARAMS?+?et?+?params.toString();???????????//通用參數(shù)的使用要注意
Log.w("NETURL",url+"?"+paramsData);
urlConnection.setRequestProperty("Connection",?"keep-alive");?????????????????????????//?設(shè)置維持長(zhǎng)連接
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");?//?設(shè)置文件的類(lèi)型
urlConnection.setRequestProperty("Content-Length",String.valueOf(paramsData.getBytes().length));?//?設(shè)置數(shù)據(jù)的長(zhǎng)度
urlConnection.setRequestProperty("User-Agent","Devond_Watch,Android-device.");??//????
urlConnection.setDoOutput(true);???//?發(fā)送POST請(qǐng)求必須設(shè)置允許輸出
urlConnection.setDoInput(true);????//?發(fā)送POST請(qǐng)求必須設(shè)置允許輸入,setDoInput的默認(rèn)值就是true
urlConnection.setUseCaches(false);?//?為安全,不允許使用緩存
urlConnection.connect();???????????//urlConnection.getInputStream()的時(shí)候會(huì)自動(dòng)的打開(kāi)連接的
OutputStream?os?=?urlConnection.getOutputStream();????//獲取輸出流developer
os.write(paramsData.getBytes());
os.flush();
if?(urlConnection.getResponseCode()?==?HttpURLConnection.HTTP_OK)?{
InputStream?is?=?urlConnection.getInputStream();??????????//?獲取響應(yīng)的輸入流對(duì)象
ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();?//?創(chuàng)建字節(jié)輸出流對(duì)象
int?len?=?0;??????????????????????????????????????????????//?定義讀取的長(zhǎng)度
byte?buffer[]?=?new?byte[2048];???????????????????????????//?定義緩沖區(qū)
while?((len?=?is.read(buffer))?!=?-1)?{???????????????????//?按照緩沖區(qū)的大小,循環(huán)讀取
baos.write(buffer,?0,?len);???????????????????????????//?根據(jù)讀取的長(zhǎng)度寫(xiě)入到os對(duì)象中
}
is.close();//?釋放資源
baos.close();
final?String?result?=?new?String(baos.toByteArray());
if(isDebug){
Log.e(TAG,"Post返回的數(shù)據(jù):"+result);
}
return?new?String(baos.toByteArray());
}else{?//返回的是HTTP的錯(cuò)誤碼,不是我們自己定義的錯(cuò)誤碼!
//處理一下http的錯(cuò)誤返回碼
Log.e(TAG,urlConnection.getResponseCode()+"#Post?網(wǎng)絡(luò)連接失敗#"+urlConnection.getErrorStream());
return?"HTTP_ERROR-"+urlConnection.getResponseCode();
}
}?catch?(MalformedURLException?e)?{
e.printStackTrace();
Log.e(TAG,"MalformedURLException?e");
}catch?(Exception?e)?{
e.printStackTrace();
}finally{
if(urlConnection!=null){
urlConnection.disconnect();
}
}
Log.e(TAG,"Post?請(qǐng)求返回為空");
return?null;
}
總結(jié)
以上是生活随笔為你收集整理的android http2.0请求,Android http HttpURLConnection的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android弹出输入框不影响布局,an
- 下一篇: android xml 删除控件,and