java接收post请求并获取数据的方法(传的json不含key)

昨天遇到一个问题

 

就是在接收post请求的时候获取不到请求数据,自己用ajax写的时候没有问题

 

这个是报文内容

{"type":"WNING_INFO","code":"WYC","downtime":"2017-01-0101:00:00","busicode":"2017021212123456","data":{"equipment_id":"1","equipment_name":"1号度计","equipment_type":"","type":"SECOND","status":"异常","warn_infor":"设障断电","location_code":"01010101","company_id":"101","company_name":"","item_code":"000001","item_name":"大米","car_no":""}}

这段报文用ajax就可发送过去

data中就是这段数据


 

可能因为是在后台发起的post的请求所以可能跟浏览器端的请求有所区别,但我到现在也没有找到区别在哪

这段代码就是发送post请求的方法

 public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("Content-Type","application/json");
            conn.setRequestProperty("charset", "utf-8");
    
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));

            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println(line);
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

 

所以就先贴出来我解决这个问题的方式

因为数据归根到底还是以数据流的方式发送过来的,所以就用流的方式来处理数据

 

这段代码就是获取HttpServletRequest中的请求数据,并解析

BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try{
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null){
                sb.append(line);
            }
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            try{
                if (null != reader){ reader.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }
        System.out.println("json;"+sb.toString());

最后得到的就是一个json字符串

 

得到这个json以后直接用Gson解析就行。

希望能帮助到碰见这个问题的人,同样也希望有人能给我解释一下出现这个问题的具体原因,谢谢

 

 

 

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值