如下:

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;
}

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

  1. Android为TV端助力:RecyclerView更新数据时焦点丢失

    1.adapter的setHasStableIds设置成true 2.重写adapter的getItemId方法 @Override public long getItemId(int positio ...

  2. Android为TV端助力 Service 两种启动方式的区别

    服务不能自己运行,需要通过调用Context.startService()或Context.bindService()方法启动服务.这两个方法都 可以启动Service,但是它们的使用场合有所不同.使 ...

  3. android post带数据请求方式,传递的数据格式包括json和map

    如下: public static String httpPost(String url, String json) { try { URL u = new URL(url); HttpURLConn ...

  4. Android为TV端助力 转载:android MVC设计模式

    Controller控制器 import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle ...

  5. Android为TV端助力 清除本应用里的各种数据的方法

    public class DataCleanManager { /** * * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * * * @param conte ...

  6. Android为TV端助力 fragment 的用法以及与activity的交互和保存数据的方法,包括屏幕切换(转载)!

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37992017 1.管理Fragment回退栈 类似与Android系统为Acti ...

  7. Android为TV端助力 转载:RecyclerView分页加载

    package com.android.ryane.pulltoloaddata_recyclerview; import android.os.Handler;import android.os.L ...

  8. Android为TV端助力转载:码农小阿飞(SpannableString)

    用SpannableString打造绚丽多彩的文本显示效果 引语 TeXtView大家应该都不陌生,文本展示控件嘛! 就用TextView显示普普通通的文本,OK,很简单,Android入门的都会,没 ...

  9. Android为TV端助力(转载)

    作者地址http://www.jianshu.com/u/63915ef020e2 针对Android Tv的自定义RecyclerView 作者 wenju_song 关注 2016.12.09 1 ...

随机推荐

  1. muduo-ThreadLocal实现细节——阻止销毁未定义对象

    muduo利用pthread_key_t实现ThreadLocal模板类. 具体代码如下所示: template<typename T> class ThreadLocal : nonco ...

  2. 必须要会的 50 个 React 面试题

    翻译:疯狂的技术宅 原文:www.edureka.co/blog/interv… 如果你是一位有抱负的前端程序员并准备面试,那么这篇文章很适合你.本文是你学习和面试 React 所需知识的完美指南. ...

  3. 【新手向】使用nodejs抓取百度贴吧内容

    参考教程:https://github.com/alsotang/node-lessons 1~5节 1. 通过superagent抓取页面内容 superagent .get('http://www ...

  4. JS 取消iOS播放自动全屏:

    iOS下浏览器模式下h5播放器强制是全屏的,除非在app下才可以非全屏播放,需要两个配置: (1)播放器添加参数: playsinline:true(我使用的是阿里云的播放器,其他的需要自己找找是那个 ...

  5. c#使用dynamic关键字传输数据的用法

    问: 在实际开发中,特别是在ORM框架的基础下,我们返回的数据都是强类型的实体对象.如果是单表查询我们就可以直接返回对应的实体,如果是多表联合查询,我们可能就需要各个表中都返回一部分字段,组成一个新的 ...

  6. Ocelot简易教程(二)之快速开始1

    Ocelot简易教程目录 Ocelot简易教程(一)之Ocelot是什么 Ocelot简易教程(二)之快速开始1 Ocelot简易教程(二)之快速开始2 Ocelot简易教程(三)之主要特性及路由详解 ...

  7. django 模板层排序 class Meta 添加信息

    class weeks(models.Model): #星期信息 stu = models.ForeignKey(students,on_delete=models.CASCADE) weeklist ...

  8. 用canvas画一个等腰三角形

    上图是代码,注意,宽高只有在canvas标签内部设置宽高,绘制的路径显示才是正常的:效果如下:

  9. ftp服务器搭建及简单操作

    ftp服务器搭建及简单操作 1. 添加一个新用户,使用名useradd testftp,然后使用passwd testftp对新添加的用户设置密码(这里设置为“1234567”). 2. 安装ftp服 ...

  10. RocketMQ源码 — 五、 主要feature及其实现方式

    RocketMQ的主要特点以及实现方式 单机支持1万以上持久队列 所有数据单独存储到一个CommitLog,完全顺序写,随机读 在一个broker上一个DefaultMessageStore管理一个c ...