短信设备猫池接收短信时m.setContent(i.getText()); 获得短信内容报错 java.lang.RuntimeException: getText() not supported
Exception in thread "SMSLib-AsyncMessageProcessor : modem.com1" java.lang.RuntimeException: getText() not supported
at org.smslib.InboundBinaryMessage.getText(InboundBinaryMessage.java:37)
at com.sdb.test.ReadAndSendSimple.actionPerformedMsg(ReadAndSendSimple.java:63)
at com.sdb.modem.SendReadMsger.msgArrive(SendReadMsger.java:87)
at com.sdb.modem.Modem.msgArrive(Modem.java:56)
at com.sdb.modem.Modem.access$0(Modem.java:55)
at com.sdb.modem.Modem$InboundNotification.process(Modem.java:221)
at org.smslib.modem.AModemDriver$AsyncMessageProcessor.run(AModemDriver.java:898)
解决方案尝试:
读取到的message要判断类型先,binary的message肯定是没有getText的方法的
if(inboundMessage instanceof InboundBinaryMessage){ if (((InboundBinaryMessage)inboundMessage).getDataBytes() != null){ String binaryString = PduUtils.bytesToPdu(((InboundBinaryMessage)inboundMessage).getDataBytes()); logger.info(binaryString); sms.setContent(binaryString); } }else if(inboundMessage instanceof InboundEncryptedMessage){ try { logger.info(((InboundEncryptedMessage) inboundMessage).getDecryptedText()); sms.setContent(((InboundEncryptedMessage) inboundMessage).getDecryptedText()); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SMSLibException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ logger.error(inboundMessage.getText()); logger.error(inboundMessage.getPduUserData()); sms.setContent(inboundMessage.getPduUserData()); }
本人没实际应测试过这个方法,但有客户已经测试,另外发现出现这个错误的原因是因为接收到一些营销彩信导致报错的,因为彩信是没有正常短信内容所以无法获取到短信内容。
有时间的可以自己下载smslib源码及开发文档介绍了解这个包的彩信功能进行修改添加,把彩信进行解析出来即可,也可以判断如果是8位编码信息直接删除短信,不给予解析理会跳过,就不在报错了。
原问题连接:https://bbs.csdn.net/topics/390466667