小程序

java.lang.NumberFormatException:

2019-09-30  本文已影响0人  牛哞哞杂记

问题:java.lang.NumberFormatException: null

解决办法:
小程序使用request方法时的content-type改成"application/x-www-form-urlencoded"

代码如下:

//test.js
 wx.request({
      url: "xxxxxx",
      method: 'POST',
      data:{ "testId": testId},
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: (res) => {
        console.log(res)
      },
    })
//Test.java
String Id = request.getParameter("testId");

详细说明:
我将请求头的类型修改成"application/x-www-form-urlencoded"之后,获取的值不为null的原因,可通过两种请求头的区别找出来
(1)application/x-www-form-urlencoded
是浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据。提交的数据按照 key1=val1&key2=val2 的方式进行编码,key 和 val 都进行了 URL 转码。
(2)application/json
把它作为请求头,是用来告诉服务端消息主体是序列化后的 JSON 字符串。
我在后台是使用"request.getParameter(“testId”)"来获取值,而它获取的类型正是 key1=val1&key2=val2的形式,因此将请求头改成“application/x-www-form-urlencoded”就可以解决这个问题了

详细错误:
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at com.szearth.servlet.PushMessage.doPost(PushMessage.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.szearth.filter.EncodeFilter.doFilter(EncodeFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocolAbstractConnectionHandler.process(AbstractProtocol.java:637)atorg.apache.tomcat.util.net.AprEndpointAbstractConnectionHandler.process(AbstractProtocol.java:637)atorg.apache.tomcat.util.net.AprEndpointSocketProcessor.doRun(AprEndpoint.java:2536)
at org.apache.tomcat.util.net.AprEndpointSocketProcessor.run(AprEndpoint.java:2525)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)atjava.util.concurrent.ThreadPoolExecutorSocketProcessor.run(AprEndpoint.java:2525)atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)atjava.util.concurrent.ThreadPoolExecutorWorker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)

文章最后发布于: 2019-09-29

上一篇下一篇

猜你喜欢

热点阅读