如下:

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. Node.js项目拆包工程化

    背景 在我们开发的过程中,经常会遇到这样的问题,开发完了一些代码或者一个接口,别的小伙伴过来问你,代码可不可以给他复用,接口可以给他调用.这说明代码的复用和抽象对团队协作是很重要的.举个例子,如下图 ...

  2. 转转hybrid app web静态资源离线系统实践

    一.前言 目前的转转app是一个典型的hybrid app,采用的是业内主流的做法: 客户端内有大量业务页面使用webview内加载h5页面承载. 其优点是显而易见的,即:web页面上线频度满足快速迭 ...

  3. Docker简介以及操作

    Docker 简介 Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Linu ...

  4. [django]date类型和datetime类型过滤

    搞清楚datetime.datetime和datetime.date模块 他们两个的格式区别 datetime模块 In [1]: from datetime import datetime In [ ...

  5. 使用Docker发布DNC项目

    项目结构 可以忽略中间三个Console项目 ApiCenter 是一个WebAPI项目,引用了NLog.MQ项目 ApiCenter使用5001端口 public static IWebHost B ...

  6. leetcode — maximum-subarray

    /** * * Source : https://oj.leetcode.com/problems/maximum-subarray/ * * Created by lverpeng on 2017/ ...

  7. shell脚本示例:计算毫秒级、微秒级时间差

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 有时候需要计算命令的执行时间长度,可以使用time命令,虽然t ...

  8. NLP入门(二)探究TF-IDF的原理

    TF-IDF介绍   TF-IDF是NLP中一种常用的统计方法,用以评估一个字词对于一个文件集或一个语料库中的其中一份文件的重要程度,通常用于提取文本的特征,即关键词.字词的重要性随着它在文件中出现的 ...

  9. 记录Newtonsoft.Json的日常用法

    最近在做一个使用基于.net mvc 实现前后台传输Json的实例.网上找了一些资料.发现在开发的时候,许多的数据交互都是以Json格式传输的.其中涉及序列化对象的使用的有DataContractJs ...

  10. Intellij idea 项目目录设置 与包的显示创建

    1.把目录设置成为层级结构显示.和eclipse类似 去掉flatten Packages前面的勾 在项目中创建多级包的时候要注意,必须在Java下建,并且要全输入才能识别