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相关推荐
- internetreadfile读取数据长度为0_Go发起HTTP2.0请求流程分析(后篇)——标头压缩
阅读建议 这是HTTP2.0系列的最后一篇,笔者推荐阅读顺序如下: Go中的HTTP请求之--HTTP1.1请求流程分析 Go发起HTTP2.0请求流程分析(前篇) Go发起HTTP2.0请求流程分析 ...
- python 使用 httpx 发送http2.0 请求
python 使用 httpx 发送http2.0 请求 摘要 安装 http/2 支持 客户端请求 更有效地利用网络资源 额外功能 同步 异步 复杂示例,APNS异步推送到多用户 http/1 支持 ...
- Android 9.0/P(android p指安卓9.0版本) okhttp3网络请求出错
已经在AndroidManifest.xml申请网络权限,在8.0以下的系统中网络访问正常,但是9.0中出现网络请求失败 Android 9.0的网络请求失败如下图: 出现这个错误的原因是:从Andr ...
- 初次Android 6.0升级Android 8.0的心酸总结
前置条件 1.需要先确定你要升级的Android版本(这里我是从6.0升级到8.0,也就是compileSdkVersion从23升级到26),无论是升级到什么版本,你的AS都至少需要升级到3.0以上 ...
- android 6.0谷歌,Android 6.0来了!谷歌月底要发布Android M系统
谷歌将在美国当地时间5月28日举办年度I/O开发者大会上.一如往年惯例,谷歌届时将会发布最新Android操作系统.虽然谷歌目前尚未直接表态,但其I/O大会日程安排却透露了蛛丝马迹. 在今年的I/O大 ...
- android 7.0 裁剪,Android 7.0中拍照和图片裁剪适配的问题详解
前言 Android 7.0系统发布后,拿到能升级的nexus 6P,就开始了7.0的适配.发现在Android 7.0以上,在相机拍照和图片裁剪上,可能会碰到以下一些错误: Process: com ...
- Android应用安装apk版本升级,适配Android 8.0和Android 10.0下载安装,shell命令安装APK
shell命令安装 /*** 安装apk** @param path apk文件路径*/ public void installAPK(String path) {Log.i(TAG, "i ...
- android9.0变化,十年巨变 Android 1.0对比Android 9
转瞬间,安卓已经成为了一个10年的老操作系统了,而安卓版本也从Android 1.0来到了Android 9,当然安卓的界面也在过去十年中发生了巨大变化. androidpolice网站找来了一台老旧 ...
- 手机中android版本9是什么,这是Android手机Android 9.0还是Android 6.0?
我想将一些评论变成一个答案,该答案可用于其他验证,以防其他人遇到类似的问题(供应商也重新设置了通知窗口). Android 9.0于2018年3月发布.安全补丁程序级别为2017年5月.这根本没有意义 ...
最新文章
- 白宫启动AI.GOV计划,呼吁各界携手共同推进AI发展
- 远控软件VNC***案例研究
- fileoutputstream 转 byte数组_Java:如何实现文件与数组的相互转换?
- codeforces524E
- 传奇的诞生,PHP三位创始人简介
- linux修改容器内的mysql端口映射_修改docker容器端口映射的方法
- php mysql 博客制作_PHP实现简易blog的制作
- 英雄联盟手游:大神开发提莫打野,伤害爆表,玩家纷纷效仿
- jzoj6800-NOIP2020.9.19模拟spongebob【枚举】
- 解聘!“双一流”教授被通报批评
- phpcmsV9框架:安装教程
- EL表达式取Map,List值的总结
- C++变量/函数命名规范
- OpenFire 安装及配置
- smart原则_设立目标的smart原则
- 正交实验法,软件测试用例的特性,编写方法,软件缺陷的基础知识
- 一个过不了情关的男人!!
- 塑造成功性格的15种方法
- inner join 和 outer join 的区别
- 验证:获取linux系统的网卡信息
热门文章
- CSerialPort多线程串口编程工具详解
- 目标文件里面到底有什么(1)?
- pandas 中的函数—— .reset_index()
- 青年歌手大奖赛_评委会打分
- 调用函数,求a+aa+aaa+....+aa...aa(n个a)
- jsp里面声明了utf-8格式,也写了字符编码过滤器,数据库编码也是utf-8,就连java.......
- JVM_06 垃圾回收相关算法 [ 一 ]
- JUC重要辅助类(同步组件及锁)
- pat天梯赛L1-051. 打折
- Binder实用指南(一) - 理解篇