如下:

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 post带数据请求方式,传递的数据格式包括json和map的更多相关文章

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

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

  2. Layui数据表格的接口数据请求方式为Get

    Layui数据表格的接口数据请求方式为Get

  3. Android五种数据存储方式

    android 五种数据存储 :SharePreferences.SQLite.Contert Provider.File.网络存储 Android系统提供了四种存储数据方式.分别为:SharePre ...

  4. Android——JDK的get请求方式

    layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...

  5. [ Android 五种数据存储方式之一 ] —— SharedPreferences存储数据

    SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. 主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceS ...

  6. c# 数据请求方式提供

    营销平台数据请求介绍 项目介绍: 前端使用 WPF,采用MVVM模式  后端数据库采用的sqlite 依靠本地化运行   后期可能会采用WebApi   因为WPF都是自学的 所以 代码方面写的可能不 ...

  7. android中的数据存取-方式一:preference(配置)

    这种方式应该是用起来最简单的Android读写外部数据的方法了.他的用法基本上和J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单. 透明的方式来保存一些用户个 ...

  8. Android下的数据储存方式(三)

      Android下最好的数据储存方式:关系型数据库sqlite.   数据库的创建:使用SqliteOpenHelper类 结合SqliteOpenHelper类和SQLiteDatabase类的帮 ...

  9. Android下的数据储存方式( 二)

    在上一篇文章中我们介绍了SharedPreferences的使用方法. 今天我们继续介绍另一种储存数据的方式:使用内部储存和外部储存 每一个Android设备都拥有两个数据储存区域:外部储存和外部储存 ...

随机推荐

  1. Tornado框架

    Tornado介绍 Tornado 是 FriendFeed 使用的可扩展的异步非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py(豆瓣用这个写的) 或者 Go ...

  2. ASP.NET MVC必知必会知识点总结(二)

    一.实现Controller的依赖注入: 1.自定义继承DefaultControllerFactory 类的控制器工厂类并重写GetControllerInstance方法:(如:InjectCon ...

  3. StreamHelper

    public static MemoryStream CreateMemoryStreamFromBytes(byte[] inputData) { if (inputData == null || ...

  4. ADO.NET基础巩固-----连接类和非连接类

          最近的一段时间自己的状态还是不错的,早上,跑步,上自习看书,下午宿舍里面编程实战,晚上要么练习代码,要么去打球(在不打就没机会了),生活还是挺丰富的. 关于C#的基础回顾就先到前面哪里,这 ...

  5. Android SDK Android NDK Android Studio 官方下载地址

    2016.12 Android Studio Windows Includes Android SDK https://dl.google.com/dl/android/studio/install/ ...

  6. When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.

    在调试SignalR程序时,提示一个异常:When using SqlDependency without providing an options value, SqlDependency.Star ...

  7. Winform混合式开发框架的特点总结

    Winform混合式开发框架,是一种支持分布式部署的应用模式,支持直接连接数据库,访问远程WCF服务,访问远程Web API服务等服务的综合性框架,根据不同的需求采用不同的数据接口,是一个适应性很广的 ...

  8. Mysql日期统计函数简介

    NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 DATE() 提取日期或日期/时间表达式的日期部分 EXTRACT() 返回日期/时间按的单独 ...

  9. iOS App上架AppStore 会遇到的坑

    前言部分 前言:非原创 文章摘自:http://zhuanlan.zhihu.com/100000PM/20010725 相信大家一定非常「深恶痛疾」AppStore的一系列产品上架规则.每次产品上架 ...

  10. Delphi iOS 开启文件共享 UIFileSharingEnabled

    Apple 在 iOS 提供了文件共享(FileSharing)功能,让 App 有一个对外窗口的目录,透过 iTunes 可以任意管理这个目录的文档内容(可拖入文档,也能将文档拖出备份). 如果 A ...