Android 开发创建 MqttAsyncClient 错误,Can‘t parse string to URI mqttAsyncClientnewMqttAsyncClient(ssl://my-example. com:2345,CLIENT_ID,newMemoryPersistence());在 Android 开发重执行上述代码出现如下错误信息java.lang.IllegalArgumentException: Cant parse string to URI ssl://my-example. com:2345问题原因URI 字符串错误异常是 IllegalArgumentException而不是 MqttException源码如下publicMqttAsyncClient(StringserverURI,StringclientId,MqttClientPersistencepersistence)throwsMqttException{this(serverURI,clientId,persistence,null,null);}publicMqttAsyncClient(StringserverURI,StringclientId,MqttClientPersistencepersistence,MqttPingSenderpingSender,ScheduledExecutorServiceexecutorService)throwsMqttException{finalStringmethodNameMqttAsyncClient;log.setResourceName(clientId);if(clientId!null){// Verify that the client ID is not too long// Encode it ourselves to checkByteArrayOutputStreambaosnewByteArrayOutputStream();DataOutputStreamdosnewDataOutputStream(baos);MqttDataTypes.encodeUTF8(dos,clientId);// Remove the two size bytes.if(dos.size()-265535){thrownewIllegalArgumentException(ClientId longer than 65535 characters);}}else{clientId;}mqttConnectionnewMqttConnectionState(clientId);NetworkModuleService.validateURI(serverURI);this.serverURIserverURI;this.mqttSession.setClientId(clientId);this.persistencepersistence;if(this.persistencenull){this.persistencenewMemoryPersistence();}this.executorServiceexecutorService;this.pingSenderpingSender;if(this.pingSendernull){this.pingSendernewTimerPingSender(this.executorService);}// TRACE 101init ClientID{0} ServerURI{1} PersistenceType{2}log.fine(CLASS_NAME,methodName,101,newObject[]{clientId,serverURI,persistence});this.persistence.open(clientId);this.commsnewClientComms(this,this.persistence,this.pingSender,this.executorService,this.mqttSession,this.mqttConnection);this.persistence.close();this.topicsnewHashtableString,MqttTopic();}publicstaticvoidvalidateURI(StringbrokerUri)throwsIllegalArgumentException{try{URIurinewURI(brokerUri);Stringschemeuri.getScheme();if(schemenull||scheme.isEmpty()){thrownewIllegalArgumentException(missing scheme in broker URI: brokerUri);}schemescheme.toLowerCase();synchronized(FACTORY_SERVICE_LOADER){for(NetworkModuleFactoryfactory:FACTORY_SERVICE_LOADER){if(factory.getSupportedUriSchemes().contains(scheme)){factory.validateURI(uri);return;}}}thrownewIllegalArgumentException(no NetworkModule installed for scheme \scheme\ of URI \brokerUri\);}catch(URISyntaxExceptione){thrownewIllegalArgumentException(Cant parse string to URI \brokerUri\,e);}}处理策略确保 URI 字符串格式正确需要同时捕获 MqttException 和 IllegalArgumentException或者可以捕获更通用的 Exceptiontry{mqttAsyncClientnewMqttAsyncClient(ssl://my-example.com:2345,CLIENT_ID,newMemoryPersistence());}catch(IllegalArgumentExceptione){...}catch(MqttExceptione){...}

相关新闻

最新新闻

商业落地评测|实体门店GEO优化性价比与服务体系深度复盘

商业落地评测|实体门店GEO优化性价比与服务体系深度复盘

摘要:对于中小实体门店与区域连锁品牌而言,GEO服务商的服务流程、定价体系、场景适配能力、售后运维质量,直接决定营销投入的ROI。本文以商家落地视角,结合数十家全品类门店合作案例,从性价比、服务闭环、场景适配、资…

2026/7/22 23:03:32
终极指南:如何在Windows上完美使用PS4手柄进行游戏

终极指南:如何在Windows上完美使用PS4手柄进行游戏

终极指南:如何在Windows上完美使用PS4手柄进行游戏 【免费下载链接】DS4Windows Like those other ds4tools, but sexier 项目地址: https://gitcode.com/gh_mirrors/ds/DS4Windows 想让你的PlayStation 4手柄在Windows电脑上获得完整的游戏体验吗&#xff1…

2026/7/22 23:03:32
Codex+ChatGPT 胜过TRAE+DeepSeek组合的感受

Codex+ChatGPT 胜过TRAE+DeepSeek组合的感受

在我应用TRAEDeepSeek V4 pro(以下简称TD组合)开发了多个应用,50万代码的开发之后。我决定尝试一下CodexChatGPT 5.6 sol的组合,以下简称CC。我提的开发需求如下:1 对英文文件进行朗读,忽略其他语言&#x…

2026/7/22 23:03:32
AI 电动搅拌杯智能功率 MOSFET 核心选型方案

AI 电动搅拌杯智能功率 MOSFET 核心选型方案

随着 AI 智能营养管理、多段位精准搅拌与温控成为高端搅拌杯标配,其内部驱动电路对功率 MOSFET 提出更高要求:高效率、高集成度、高可靠性。微碧半导体(VBsemi)基于成熟的 Trench 工艺,为您提供覆盖电机驱动、电源管理…

2026/7/22 23:03:32
3大核心功能:defuddle 智能网页内容提取的终极解决方案

3大核心功能:defuddle 智能网页内容提取的终极解决方案

3大核心功能:defuddle 智能网页内容提取的终极解决方案 【免费下载链接】defuddle Get the main content of any page as Markdown. 项目地址: https://gitcode.com/gh_mirrors/de/defuddle 在信息爆炸的时代,你是否曾为网页中无处不在的广告、侧…

2026/7/22 23:03:32
构建经典技术博客库:筛选、存储与价值挖掘

构建经典技术博客库:筛选、存储与价值挖掘

1. 项目概述:经典博客链接的价值与意义在信息爆炸的时代,我们每天都被海量内容淹没。作为一个从业十多年的内容创作者,我深刻体会到优质内容被埋没的痛心。那些经过时间检验的经典博客文章,往往蕴含着超越时代的智慧结晶。它们可能…

2026/7/22 22:58:31

月新闻