起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3570|回复: 14

[处理中3] 获取Content-type为application/json类型的传参不成功

[复制链接]

39

主题

168

帖子

421

积分

中级会员

Rank: 3Rank: 3

积分
421
QQ
发表于 2017-3-19 18:18:45 | 显示全部楼层 |阅读模式
//HTTP头部Content-type为application/json,而非大部分框架自动解析的application/x-www-form-urlencoded格式,需要自行读取后解析


public static JSONObject service(JSONObject params, ActionContext context) throws ServletException, IOException{
                HttpServletRequest req = (HttpServletRequest)context.get(ActionContext.REQUEST);
                HttpServletResponse resp = (HttpServletResponse)context.get(ActionContext.RESPONSE);
                receivePost(req);
                return null;
        }


public static String receivePost(HttpServletRequest req) throws IOException, UnsupportedEncodingException {      
        // 读取请求内容
        BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }


        // 将资料解码返回
        String reqBody = sb.toString();
        System.out.println("resp:"+reqBody);
        return reqBody;   //URLDecoder.decode(reqBody, HTTP.UTF_8);
    }

获取不到参数,怎么处理?
发表于 2017-3-20 13:18:54 | 显示全部楼层
首先,请问你需要返回值吗?如果需要service 方法中不要 return null;
还有 你receivePost 方法中return reqBody;   实际上没有任何作用!!并没有使用这个返回值!!

关于输入输出流的代码!建议上网搜一下吧!!想使用java实现什么能力??
剩下的就是自己调试java代码,实现自己想要完成的事情
http://docs.wex5.com/debug-user-own-java-code/
http://docs.wex5.com/wex5-studio-question-list-2001/

调试过程中看一下哪儿不符合你的预期??
qq:1912779713
WeX5教程--WeX5下载
回复 支持 反对

使用道具 举报

39

主题

168

帖子

421

积分

中级会员

Rank: 3Rank: 3

积分
421
QQ
 楼主| 发表于 2017-3-20 14:09:45 | 显示全部楼层
需要返回值,下一步还要根据返回值处理,网上搜了一把,都不行,放在后面再慢慢折腾吧
回复 支持 反对

使用道具 举报

发表于 2017-3-20 15:47:57 | 显示全部楼层
sweetluo 发表于 2017-3-20 14:09
需要返回值,下一步还要根据返回值处理,网上搜了一把,都不行,放在后面再慢慢折腾吧 ...

application/json类型 表示传递参数类型为json对象!或者参数值应该写法是
params.getString("key")
只有上传文件的时候,才需要 req.getInputStream()  
才需要IO流操作的吧!!json格式的数据不需要通过IO输入获取吧!
qq:1912779713
WeX5教程--WeX5下载
回复 支持 反对

使用道具 举报

39

主题

168

帖子

421

积分

中级会员

Rank: 3Rank: 3

积分
421
QQ
 楼主| 发表于 2017-3-20 16:54:50 | 显示全部楼层
application/x-www-form-urlencoded格式的可用下面的
String tradeStatus = req.getParameter("trade_status");
回复 支持 反对

使用道具 举报

39

主题

168

帖子

421

积分

中级会员

Rank: 3Rank: 3

积分
421
QQ
 楼主| 发表于 2017-3-20 16:55:56 | 显示全部楼层
application/json类型 还是取不到
回复 支持 反对

使用道具 举报

发表于 2017-3-20 17:03:13 | 显示全部楼层
sweetluo 发表于 2017-3-20 16:54
application/x-www-form-urlencoded格式的可用下面的
String tradeStatus = req.getParameter("trade_statu ...

baasServer  自定义action 传参和接受参数请参考:
http://docs.wex5.com/wex5-ui-question-list-2098/
写法不一样!不是直接在request  中获取的!
qq:1912779713
WeX5教程--WeX5下载
回复 支持 反对

使用道具 举报

39

主题

168

帖子

421

积分

中级会员

Rank: 3Rank: 3

积分
421
QQ
 楼主| 发表于 2017-3-20 20:49:20 | 显示全部楼层

传入参数:
HTTP 请求类型 : POST
HTTP 数据格式 : JSON
HTTP Content-type : application/json

字段说明KeyTypeExample
signatureString32位小写
timestampLong1426817510111
channel_typeString‘WX’ or 'ALI’ or 'UN’ or 'KUAIQIAN’ or 'JD’ or 'BD’ or 'YEE’ or 'PAYPAL’ or 'BC’
sub_channel_typeString'WX_APP’ or 'WX_NATIVE’ or 'WX_JSAPI’ or 'WX_SCAN’ or 'ALI_APP’ or 'ALI_SCAN’ or 'ALI_WEB’ or 'ALI_QRCODE’ or 'ALI_OFFLINE_QRCODE’ or 'ALI_WAP’ or 'UN_APP’ or 'UN_WEB’ or 'PAYPAL_SANDBOX’ or 'PAYPAL_LIVE’ or 'JD_WAP’ or 'JD_WEB’ or 'YEE_WAP’ or 'YEE_WEB’ or 'YEE_NOBANKCARD’ or 'KUAIQIAN_WAP’ or 'KUAIQIAN_WEB’ or 'BD_APP’ or 'BD_WEB’ or 'BD_WAP’ or 'BC_TRANSFER’ or 'ALI_TRANSFER’
transaction_typeString'PAY’ or 'REFUND’ or 'TRANSFER'(TRANSFER只代表BC企业打款和支付宝打款;微信打款和红包,没有webhook)
transaction_idString'201506101035040000001’
transaction_feeInteger1 表示0.01元
trade_successBooltrue
message_detailMap(JSON){orderId:xxxx}
optional
Map(JSON)
{“agent_id”:“Alice”}

==========================================================
如何接收?
StringBuffer json = new StringBuffer();
                    String line = null;
                    

                    try {
                            req.setCharacterEncoding("utf-8");
                        BufferedReader reader = ((ServletRequest) req).getReader();
                            //BufferedReader reader = req.getReader();
                        while ((line = reader.readLine()) != null) {
                            json.append(line);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


java.lang.IllegalStateException: getInputStream() has already been called for this request


================================================
JSONArray userCode = (JSONArray) params.get(req);
            Object [] o =userCode.toArray();
            for(int i=0;i<o.length;i++){
                System.out.println("--test------"+o);
            }

三月 20, 2017 8:41:06 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet WeX5_Baas_Servlet threw exception
java.lang.NullPointerException
        at beecloud.Receiver.service(Receiver.java:52)
        at beecloud.Beecloud__do.receiver(Beecloud__do.java:107)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.justep.baas.action.Engine.execAction(Unknown Source)
        at com.justep.baas.action.Engine.execAction(Unknown Source)
        at com.justep.baas.servlet.BaasServlet.execService(Unknown Source)
        at com.justep.baas.servlet.BaasServlet.service(Unknown Source)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:745)

2017-03-20 20:41:06 Action[beecloud/beecloud/receiver]执行失败,null
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.justep.baas.action.Engine.execAction(Unknown Source)
        at com.justep.baas.action.Engine.execAction(Unknown Source)
        at com.justep.baas.servlet.BaasServlet.execService(Unknown Source)
        at com.justep.baas.servlet.BaasServlet.service(Unknown Source)


=================================================
JSONArray userCode = (JSONArray) params.get("optional");
            Object [] o =userCode.toArray();
            for(int i=0;i<o.length;i++){
                System.out.println("--test------"+o);
            }


严重: Servlet.service() for servlet WeX5_Baas_Servlet threw exception
java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.alibaba.fastjson.JSONArray
        at beecloud.Receiver.service(Receiver.java:51)
        at beecloud.Beecloud__do.receiver(Beecloud__do.java:107)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.justep.baas.action.Engine.execAction(Unknown Source)
        at com.justep.baas.action.Engine.execAction(Unknown Source)
        at com.justep.baas.servlet.BaasServlet.execService(Unknown Source)
        at com.justep.baas.servlet.BaasServlet.service(Unknown Source)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:745)

2017-03-20 20:43:40 Action[beecloud/beecloud/receiver]执行失败,com.alibaba.fastjson.JSONObject cannot be cast to com.alibaba.fastjson.JSONArray
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.justep.baas.action.Engine.execAction(Unknown Source)


回复 支持 反对

使用道具 举报

39

主题

168

帖子

421

积分

中级会员

Rank: 3Rank: 3

积分
421
QQ
 楼主| 发表于 2017-3-20 20:54:05 | 显示全部楼层
https://github.com/beecloud/beec ... ebhook_receiver.jsp

找到的说明,怎么将这个改成WEX5可用的?
回复 支持 反对

使用道具 举报

发表于 2017-3-21 11:30:39 | 显示全部楼层
sweetluo 发表于 2017-3-20 20:54
https://github.com/beecloud/beecloud-java/blob/master/demo/WebRoot/webhook_receiver_example/webhook_ ...

案例中,获取参数的写法:(JSONArray) params.get("code");
code是前端传递过来的参数啊!!你写的是
(JSONArray) params.get(req);
请问哪儿见过这种写法???传递的参数是req 对象??

真的不知道你想实现什么??请把需求说明白一些!!!

如果是使用BaasServer 的请求 以及baas.js的方法进行传参和接收参数的!就按照案例的书写格式实现!!

如果是使用ajax 实现的请求BaasServer ,并且需要通过获取HttpServletRequest 对象来获取值的!就按照通用的方法获取参数!
但是请不要混合使用!!

不要用baas.js的请求 ,和正常的HttpServletRequest 对象 传参和接收参数!
qq:1912779713
WeX5教程--WeX5下载
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2025-7-4 23:47 , Processed in 0.060195 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表