try {
//创建连接
URL url = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
// POST请求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
JSONObject obj = new JSONObject();
String json = java.net.URLEncoder.encode(obj.toString(), "utf-8");
out.writeBytes(json);
out.flush();
out.close();
// 读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = URLDecoder.decode(lines, "utf-8");
sb.append(lines);
}
System.out.println(sb);
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

HttpURLConnection传JSON数据的更多相关文章

  1. 【代码笔记】iOS-向服务器传JSON数据的两种方式

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  2. [iOS 多线程 & 网络 - 2.6] - 使用POST上传JSON数据 & 多值参数

    A.上传JSON 1.思路: 必须使用POST方法才能上传大量JSON数据 设置请求头:设置Content-Type 设置请求体,JSON实际相当于字典,可以用NSDictionary NSJSONS ...

  3. springmvc04-文件上传-JSON数据

    文件上传部分: 1, 导入commons-fileupload-1.2.2.jar commons-io-2.4.jar 两个jar包. 2, 在主配置文件中,添加如下信息 <!-- 文件上传- ...

  4. POST 上传 JSON 数据

    // // ViewController.m // 03-post上传json // // Created by jerry on 15/10/10. // Copyright (c) 2015年 j ...

  5. Http请求传json数据中文乱码问题

    业务场景:调easyui的dialog打开一个弹窗,传参是用json封装的,而且有中文,然后在极速模式是正常的,在ie11测试发现中文出现乱码了 var params = JSON.stringify ...

  6. 静态页面中如何传json数据

    首页传递参数组装成json数据,再编码 var param="{type:'"+type+"',text:'"+select_text+"',sele ...

  7. HttpURLConnection传json

    private static String sendToWangTing(DataRow dataRow) throws IOException{ String ip = Configuration. ...

  8. HttpUrlConneciton上传JSON数据

    try { //创建连接 URL url = new URL(url); HttpURLConnection connection = (HttpURLConnection) url.openConn ...

  9. php用get方式传json数据 变成null了

    $data = I('param.data'); $data=stripslashes(html_entity_decode($data));//$data为传过去的json字符串

随机推荐

  1. ant svn

    <!-- 检出代码 这里使用 export 不是checkout 二者区别 checkout会svn相关信息文件检出,export只是检出最新的文件--> <target name= ...

  2. 2014.3.6-C语言学习小结

    链表基础: 知识点: 1.链表基础 2.节点的创建和添加 llist_append_node 3.链表的遍历 llist_print_each 4.链表的查找与修改 5.链表的插入与删除 6.链表的销 ...

  3. SZU:B85 Alec's Eggs

    Description Eggs Alec has a lot of eggs. One day, he want to sort them in a ascending sequence by we ...

  4. iOS基础 - 触摸事件&手势识别

    ================================================================== 一.触摸事件&手势识别 1> 4个触摸事件,针对视图 ...

  5. Android编程心得-在任意类中获取当前屏幕宽高

    进行Android编程时,很多时候都需要获取当前屏幕的宽度与高度,但是当我们需要在别的类中调用屏幕宽高时,直接用原来的方法是不行的,下面我来介绍如何在任意类中调用宽度高度的两种方法. public v ...

  6. C++中内存泄露的检测

    C++没有java的内存垃圾回收机制,在程序短的时候可能比较容易发现问题,在程序长的时候是否有什么检测的方法呢? 假设有一个函数可以某点检测程序的内存使用情况,那是否可以在程序开始的时候设置一个点,在 ...

  7. js中的屏蔽

    js屏蔽效果 /** 屏蔽F1帮助 */  window.onhelp = function(){return false;}     /**  *屏蔽 F5.Ctrl+N.Shift+F10.Alt ...

  8. 任务分配book

    上次写某道findpath的时候,没有写个二分答案没有过,之后就发现自己貌似一开始学分治那一块的时候就没有把二分学好....就打算重新回顾一下 这道题还算水,就是太久没有写模拟wa了3次....对自己 ...

  9. VirtualBox安装CentOS6.4(32bit)

    实验环境 Win7 64bit 目的: 实验VirtualBox安装CentOS6.4(32bit) 下载VirtualBox 地址: http://www.oracle.com/technetwor ...

  10. Linux centOS本地DNS安装

    centOS本地DNS安装 在centOS里最常用的DNS服务工具应该是bind了.下面就以bind为例做一个DNS服务. 首先查看bind 是否已经安装 Rpm -qa | gerp bind 如果 ...