如下:

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

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

  1. Android为TV端助力 最详细的动画大全,包括如何在代码和在XML中使用

    一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画 ...

  2. Android为TV端助力 转载:android自定义view实战(温度控制表)!

    效果图 package cn.ljuns.temperature.view; import com.example.mvp.R; import android.content.Context; imp ...

  3. Android为TV端助力 浅谈Aidl 通讯机制

    服务端: 首先是编写一个aidl文件,注意AIDL只支持方法,不能定义静态成员,并且方法也不能有类似public等的修饰符:AIDL运行方法有任何类型的参数和返回值,在java的类型中,以下的类型使用 ...

  4. Android为TV端助力之热修复原理

    通过源码我们知道Android加载类是通过ClassLoad类里面的findClass先去查找的,如下图所示 通过看源码我们知道,ClassLoad是一个抽象类,它本身并没有实现findclass() ...

  5. Android为TV端助力context转换类型

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

  6. Android为TV端助力 同时setTag两次,保存多种值

    示例代码: view.setTag(R.string.action_settings,hodler.content); 接收两个值,一个是key值,必须是唯一值,而且要写在values/ids.xml ...

  7. Android为TV端助力 转载:Java 泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest {2 3 public static void main(Strin ...

  8. Android 手机TV端屏幕共享助手

    最近一段时间辞职在家,无聊写了个小程序也算是个demo.这里和大家分享一下,还望各位同仁多多指点.主要功能是实现同局域网下,手机端和TV端连接并分享手机屏幕画面(手机端只支持Android 5.0级以 ...

  9. vue之数据请求方式

    vue之数据请求方式 1.vue-resource 2.axios 3.fetch-jsonp 一.vue-resource 1. 安装vue-resource 在项目根目录进行安装:cnpm ins ...

最新文章

  1. 【直播预告】7月18日3D游戏引擎免费公开课答疑,參与送C币!
  2. python3并发编程基础
  3. HikariCP为什么自己造了一个FastList?
  4. 【LeetCode1046】最后一块石头的重量(堆heap)
  5. STM32工作笔记004---了解高速版PCB设计Cadence
  6. 【CCCC】L2-003 月饼 (25分),贪心
  7. WhereHows前后端配置文件
  8. Servlet面试题18道
  9. sqlserver查询语句实例
  10. DX9b 与 DX9c 在x文件解析方面带来的麻烦
  11. 苹果怎么信任企业级开发者_苹果企业签名是什么意思?
  12. 给跪了!见过最高逼格的项目总结报告!
  13. 置换密码及其python实现
  14. bcc语料库下载_BCC语料库使用指南
  15. 【ReactJs学习笔记总目录】
  16. error An unexpected error occurred: “EPERM: operation not permitted, unlink ‘C:\\Users
  17. python英语词汇量测试_python英语单词测试小程序
  18. Centos 7创建软连接,硬连接的方法
  19. 免费音视频格式转换软件
  20. 【中文题库】CISCO CCNP题库 642-892(P3.23)中文解释

热门文章

  1. eslint关闭===替换==;eslint关闭全等于校验;eslint关闭==校验
  2. 前端学习(3305):函数组件usermemo和usercallback
  3. [html] 举例说明当我们在写布局时,都有哪些边界的情况需要关注的?
  4. [html] 你有使用过time标签吗?说说它的用途有哪些?
  5. 前端学习(2900):微信小程序简介
  6. [vue] 你有写过自定义组件吗?
  7. [vue] 在移动端使用vue,你觉得最佳实践有哪些?
  8. [css] 请描述margin边界叠加是什么及解决方案
  9. 工作307:uni-富文本的实现逻辑跳转
  10. 前端学习(2449):发布文章组件