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();
}
复制代码

HttpUrlConneciton上传JSON数据的更多相关文章

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

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

  2. POST 上传 JSON 数据

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

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

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

  4. php curl 上传json数据

    PUT $data = array('username'=>'dog','password'=>'tall'); $data_json = json_encode($data); $ch ...

  5. C语言采用socket实现http post方式上传json数据

    1.按照HTTP协议发送请求: http POST 报文格式 http 报文是面向文本的. 报文分为:请求报文和响应报文 请求报文由:请求行,请求头部,空行和请求数据四个部分组成. <1.请求行 ...

  6. xUtils怎么post请求上传json数据

    InfoSmallCodeBinding smallCode = new InfoSmallCodeBinding(); smallCode.setSmallCode("测试"); ...

  7. (24)ajax上传json格式的数据

    urs.py from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpat ...

  8. Retrofit 2.0 超能实践(三),轻松实现文件/多图片上传/Json字符串

    文:http://blog.csdn.net/sk719887916/article/details/51755427 Tamic 简书&csdn同步 通过前两篇姿势的入门 Retrofit ...

  9. Retrofit 2.0 轻松实现多文件/图片上传/Json字符串/表单

    如果嫌麻烦直接可以用我封装好的库:Novate: https://github.com/Tamicer/Novate 通过对Retrofit2.0的前两篇的基础入门和案例实践,掌握了怎么样使用Retr ...

随机推荐

  1. react获取当前页面的url参数

    react获取当前页面的url参数,必须在url路由对应的组件上获取,在子组件上获取不到,为undefined,获取形如  /news/:id  的后面的参数 id this.props.match. ...

  2. 《objective-c基础教程》学习笔记(五)—— 继承方法

    在上一篇博文中,我们将原先的纯C语言代码,编写成了用Objective-C(后面直接缩写成OC)的写法.使得代码在易读性上有明显提升,结构也更清晰.同时,也对面向对象的概念有了进一步的介绍和加深. 但 ...

  3. python 字符串编码解码和格式化问题

    转自:https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868191962 ...

  4. css - Grid网格布局

    .wrapper{ display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 1 ...

  5. Spring的标签和验证等模块

    使用了spring,真的可以简化很多开发,但前提是你懂spring技术,并且环境,架包没错. 今天编写了登录验证的模块,但是就是验证不了,不知道哪里出错了,但是也不好改错,这样还是很费时间的. 错误提 ...

  6. H - The Frog's Games

    The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...

  7. G - Supermarket

    A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold ...

  8. perl 读取一个文件 替换文件的关键词 把数据替换到新的文件

    replace # replace #!/usr/bin/perl my @data = (); my ($fname ,$rp, $nfname)= @ARGV; my ($o, $n) = spl ...

  9. ThinkPHP框架 自定义 Empty 方法保护本地信息不被暴露!!!

    在使用ThinkPHP框架开发过程中,在每个Controller文件夹里面都要设置一个空控制器,用来保护本地信息不被泄露(EmptyController.class.php) 此方法很有效的隐藏系统内 ...

  10. 更新快排中的partition

    这一次是将partition 过程中, 维护三个区域. <x   =x  >x  三区域. 还有个待定的区域. /* * 将数组划分为三个分区, 小于arr[R], 等于arr[R], 大 ...