Android为TV端助力 post带数据请求方式,传递的数据格式包括json和map

如下:

public static String httpPost(String url, String json) {
try {
URL u = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
httpURLConnection.setConnectTimeout(TIMEOUT);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);

httpURLConnection.setRequestProperty("Content-Type",
"application/json");

httpURLConnection.setRequestProperty("Content-Length",
String.valueOf(json.getBytes().length));

OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(json.getBytes());

int response = httpURLConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
InputStream inptStream = httpURLConnection.getInputStream();
return dealResponseResult(inptStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

private static String dealResponseResult(InputStream inputStream) {
String resultData = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
resultData = new String(byteArrayOutputStream.toByteArray());
return resultData;
}

如果传的值不是json格式,而是map就可以采取下面这种格式

public static String httpPost(String url, Map<String, String> params) {
byte[] data = getRequestData(params, "utf-8").toString().getBytes();
try {
URL u = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) u.openConnection();
httpURLConnection.setConnectTimeout(TIMEOUT);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);

httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

httpURLConnection.setRequestProperty("Content-Length",
String.valueOf(data.length));

OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data);

int response = httpURLConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
InputStream inptStream = httpURLConnection.getInputStream();
return dealResponseResult(inptStream);
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

private static StringBuffer getRequestData(Map<String, String> params,
String encode) {
StringBuffer stringBuffer = new StringBuffer();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
stringBuffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1); // remove the last "&"
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer;
}

转载于:https://www.cnblogs.com/xiaoxiaing/p/5383688.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/418677.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(1869)vue之电商管理系统电商系统之配置axios发出登录请求

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vueVue.use(Router)export default new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login}] }) login.vue <template><div c…

linux nginx安装

https://blog.csdn.net/qq_42815754/article/details/82980326

前端学习(1870)vue之电商管理系统电商系统之配置message全局弹框组件

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vueVue.use(Router)export default new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login}] }) login.vue <template><div c…

使用贝赛尔路径(UIBezierPath)创建画板

在iOS开发中&#xff0c;创建图形&#xff0c;经常会使用贝塞尔路径&#xff0c;用于描绘一些比较复杂的图形。 使用贝塞尔路径&#xff0c;需要在view中的方法- (void)drawRect:(CGRect)rect中进行描绘。 1 - (void)drawRect:(CGRect)rect{2 UIBezierPath *path [UIBezie…

Java 8 中 Map 骚操作之 merge() 的用法

https://juejin.im/post/5d9b455ae51d45782b0c1bfb

前端学习(1871)vue之电商管理系统电商系统之路由导航守卫控制页面访问权限

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue Vue.use(Router)export default new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{…

cd命令

当我们看到一个文件夹&#xff0c;想进去瞧一瞧是什么时&#xff0c;cd命令就能满足你的好奇心。 想进去gg里面瞧一瞧&#xff0c;ok。 eg&#xff1a;cd gg 结果如下 好了&#xff0c;进入到了gg里面&#xff0c;我们要看里面的dx我用到了ll命令&#xff0c;这个命令可以显示里…

bootstrapTable基础操作

https://blog.csdn.net/u012149181/article/details/83019067

强连通分量

强连通分量是有向图中的概念&#xff0c;就是每一个顶点到其它点都由路径&#xff0c;注意有方向. 有向图强连通分量&#xff1a;在有向图G中&#xff0c;如果两个顶点vi,vj间&#xff08;vi>vj&#xff09;有一条从vi到vj的有向路径&#xff0c;同时还有一条从vj到vi的有向…

前端学习(1872)vue之电商管理系统电商系统之完善登录之后的操作

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

代码审计之配置

register_globals:php<5.4.0 PHP_INI_ALL:可在任何地方设置<?phpif($user admin){echo true;}?> allow_url_include:在5.2.0及以后默认off PHP_INI_ALL:可在任何地方设置&#xff08;allow_url_fopen&#xff09;<?phpinclude $_GET[a];?>ahttp://127.0.0.…

前端学习(1873)vue之电商管理系统电商系统之实现退出功能

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

wangeditor修改图片上传和视频上传

视频上传:https://blog.csdn.net/m0_37885651/article/details/83656437 https://www.cnblogs.com/wjwj/p/10871350.html 图片批量上传: https://blog.csdn.net/kongfanyu/article/details/85706834

深入理解构造函数和原型链

使用构造函数初始化对象的过程function Base(){} //函数声明var basenew Base() //对象初始化执行Base()的过程&#xff1a; 创建一个名为Base的function对象&#xff1a; 执行function Base(){函数体}本质上相当于var Basenew Function("函数体")&#xff1b;所以Ba…

前端学习(1874)vue之电商管理系统电商系统之处理项目中eslint语法报错

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

eclipse 安装git

eclipse 安装git&#xff1a;https://blog.csdn.net/u012604299/article/details/82390468 windows安装gitlab客户端:https://www.cnblogs.com/xwgcxk/p/9209237.html GitLab添加ssh-key&#xff0c;操作无需每次输入账号密码&#xff1a;https://blog.csdn.net/qq_40861368/…

Android一个自定义的进度环:ProgressChart

源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/ProgressChart.zip 因项目需要,自己尝试定义了一个进度环,用于显示进度,实现效果如下: 主要代码如下: public class ProgressChart extends View {private Context context;//圆环背景画笔private Paint pain…

前端学习(1876)vue之电商管理系统电商系统之整体布局

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…