android http2.0请求,Android http HttpURLConnection
/**
*?Http?get?請求
*
*?@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()?;?//通用參數的設置
Log.w("NETURL","geturl:"+urlPath);
URL?url?=?null;
HttpURLConnection?urlConnection?=?null;
try?{
ByteArrayOutputStream?outStream?=?new?ByteArrayOutputStream();
byte[]?data?=?new?byte[2048];????//應該不會小吧!
int?len?=?0;
url?=?new?URL(urlPath);
urlConnection?=?(HttpURLConnection)?url.openConnection();
//???Class>?clazz=url.openConnection().getClass();??//看看返回什么鬼類型
//???Class>?clazzSuper=clazz.getSuperclass();???????//父類又是什么鬼啊!
//getInputStream暗中執行了連接,不需要urlConnection.connect();
urlConnection.setReadTimeout(readTimeout);??????????????????????????????????????????????????//?設置請求的超時時間
urlConnection.setConnectTimeout(connectTimeout);
if?(urlConnection.getResponseCode()?==?HttpURLConnection.HTTP_OK)?{
InputStream?inStream?=?urlConnection.getInputStream();???//獲取輸入流,此時才真正建立鏈接
while?((len?=?inStream.read(data))?!=?-1)?{
outStream.write(data,?0,?len);
}
inStream.close();
if(isDebug){Log.e(TAG,"get?返回數據"+new?String(outStream.toByteArray()));}
return?new?String(outStream.toByteArray());
}else{?//返回的是HTTP的錯誤碼,不是我們自己定義的錯誤碼!
//處理一下http的錯誤返回碼
Log.e(TAG,"get?網絡連接失敗"+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();??//總是需要關閉的吧!
}
}
return?null;
}
/**
*?Http?Post?請求
*
*?@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;???????????????????????????????????????????????????????//?根據地址創建URL對象
HttpURLConnection?urlConnection?=?null;???????????????????????????????//?根據URL對象打開鏈接
try?{
url?=?new?URL(ru.getUrl());??????????????????????????????????????????????????????????//?根據地址創建URL對象
urlConnection?=?(HttpURLConnection)?url.openConnection();????????????????????????????//?根據URL對象打開鏈接
urlConnection.setRequestMethod("POST");???????????????????????????????????????????????//?設置請求的方式
urlConnection.setReadTimeout(readTimeout);????????????????????????????????????????????//?設置請求的超時時間
urlConnection.setConnectTimeout(connectTimeout);
String?paramsData?=?HttpURLConstant.COMMON_PARAMS?+?et?+?params.toString();???????????//通用參數的使用要注意
Log.w("NETURL",url+"?"+paramsData);
urlConnection.setRequestProperty("Connection",?"keep-alive");?????????????????????????//?設置維持長連接
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");?//?設置文件的類型
urlConnection.setRequestProperty("Content-Length",String.valueOf(paramsData.getBytes().length));?//?設置數據的長度
urlConnection.setRequestProperty("User-Agent","Devond_Watch,Android-device.");??//????
urlConnection.setDoOutput(true);???//?發送POST請求必須設置允許輸出
urlConnection.setDoInput(true);????//?發送POST請求必須設置允許輸入,setDoInput的默認值就是true
urlConnection.setUseCaches(false);?//?為安全,不允許使用緩存
urlConnection.connect();???????????//urlConnection.getInputStream()的時候會自動的打開連接的
OutputStream?os?=?urlConnection.getOutputStream();????//獲取輸出流developer
os.write(paramsData.getBytes());
os.flush();
if?(urlConnection.getResponseCode()?==?HttpURLConnection.HTTP_OK)?{
InputStream?is?=?urlConnection.getInputStream();??????????//?獲取響應的輸入流對象
ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();?//?創建字節輸出流對象
int?len?=?0;??????????????????????????????????????????????//?定義讀取的長度
byte?buffer[]?=?new?byte[2048];???????????????????????????//?定義緩沖區
while?((len?=?is.read(buffer))?!=?-1)?{???????????????????//?按照緩沖區的大小,循環讀取
baos.write(buffer,?0,?len);???????????????????????????//?根據讀取的長度寫入到os對象中
}
is.close();//?釋放資源
baos.close();
final?String?result?=?new?String(baos.toByteArray());
if(isDebug){
Log.e(TAG,"Post返回的數據:"+result);
}
return?new?String(baos.toByteArray());
}else{?//返回的是HTTP的錯誤碼,不是我們自己定義的錯誤碼!
//處理一下http的錯誤返回碼
Log.e(TAG,urlConnection.getResponseCode()+"#Post?網絡連接失敗#"+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?請求返回為空");
return?null;
}
總結
以上是生活随笔為你收集整理的android http2.0请求,Android http HttpURLConnection的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android弹出输入框不影响布局,an
- 下一篇: android设置通知在屏幕横幅显示,A