android mqtt ssl connection not working in android 7 -
i working on mqtt ssl connection using paho(https://github.com/eclipse/paho.mqtt.android). connection not working in android nougat , working bellow android 7.
i getting following exception:
mqttjavax.net.ssl.sslhandshakeexception: java.security.cert.certpathvalidatorexception: trust anchor certification path not found.
i using bellow code connect mqtt.
final mqttandroidclient mqttandroidclient = new mqttandroidclient(mainactivity.this, "ssl://" + pref.getmqtturl(), clientid, persistence); try { string clientid = mqttclient.generateclientid(); mqttconnectoptions connectionoptions = new mqttconnectoptions(); connectionoptions.setcleansession(true); log.e("test", "ssl://" + pref.getmqtturl()); try { inputstream truststoresis = context.getresources().openrawresource(r.raw.ca_key); string truststoretype = keystore.getdefaulttype(); keystore truststore = keystore.getinstance(truststoretype); truststore.load(truststoresis, context.getstring(r.string.bks_password).tochararray()); string tmfalgorithm = trustmanagerfactory.getdefaultalgorithm(); trustmanagerfactory tmf = trustmanagerfactory.getinstance(tmfalgorithm); tmf.init(truststore); inputstream keystorestream = context.getresources().openrawresource(r.raw.user_cer_key); keystore keystore = null; keystore = keystore.getinstance("bks"); keystore.load(keystorestream, context.getstring(r.string.bks_password).tochararray()); keymanagerfactory keymanagerfactory = null; keymanagerfactory = keymanagerfactory.getinstance(keymanagerfactory.getdefaultalgorithm()); keymanagerfactory.init(keystore, context.getstring(r.string.bks_password).tochararray()); sslcontext context = sslcontext.getinstance("ssl"); context.init(keymanagerfactory.getkeymanagers(), tmf.gettrustmanagers(), null); sslsocketfactory sslsocketfactory = (sslsocketfactory) context.getsocketfactory(); connectionoptions.setsocketfactory(sslsocketfactory); } catch (keymanagementexception | certificateexception | keystoreexception | ioexception | nosuchalgorithmexception | unrecoverablekeyexception e) { // todo auto-generated catch block e.printstacktrace(); } mqttandroidclient.connect(connectionoptions, null, new imqttactionlistener() { @override public void onsuccess(imqtttoken asyncactiontoken) { log.e("mqtt","connection success!"); } @override public void onfailure(imqtttoken asyncactiontoken, throwable exception) { log.e("mqtt","connection failure!"); } }); mqttandroidclient.setcallback(new mqttcallback() { @override public void connectionlost(throwable cause) { log.e("mqtt","connection lost!"); } @override public void messagearrived(string topic, mqttmessage message) throws exception { } @override public void deliverycomplete(imqttdeliverytoken token) { system.out.println("mqtt delivery complete!"); } }); } catch (exception ex) { ex.printstacktrace(); }
Comments
Post a Comment