2017-06-21 10 views
0

我從Android應用程序不連接Azure的IOT中心

https://github.com/Azure/azure-iot-sdk-java/tree/master/device/iot-device-samples/android-sample

克隆的Android應用程序,創造了IOT中心遵醫囑這裏

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-getstarted

我得到Connect因爲

我的代碼中提到離子串是波紋管

String connString = "HostName=********.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=FYhyjH***************c1rQ22g="; 

IotHubClientProtocol protocol = IotHubClientProtocol.MQTT; 

    DeviceClient client = new DeviceClient(connString, protocol); // error at this line 

    try { 
     client.open(); 
    } 
    catch(IOException e1) 
    { 
     System.out.println("Exception while opening IoTHub connection3: " + e1.toString()); 
    } 
    catch(Exception e2) 
    { 
     System.out.println("Exception while opening IoTHub connection4: " + e2.toString()); 
    } 

但是,同時運行的應用程序收到此錯誤,請指正

06-21 17:29:13.865 12640-12640/com.ccc.iot_demo I/System.out: Exception while opening IoTHub connection2: java.lang.IllegalArgumentException: Device ID cannot be null. 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err: java.lang.IllegalArgumentException: Device ID cannot be null. 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at com.microsoft.azure.sdk.iot.device.IotHubConnectionString.validateTerms(IotHubConnectionString.java:206) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at com.microsoft.azure.sdk.iot.device.IotHubConnectionString.<init>(IotHubConnectionString.java:90) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at com.microsoft.azure.sdk.iot.device.DeviceClient.<init>(DeviceClient.java:141) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at com.ccc.iot_demo.MainActivity.SendMessage(MainActivity.java:52) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at com.ccc.iot_demo.MainActivity.onCreate(MainActivity.java:33) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at android.app.Activity.performCreate(Activity.java:6259) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
06-21 17:29:13.865 12640-12640/com.ccc.iot_demo W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:102) 
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:  at android.os.Looper.loop(Looper.java:148) 
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5443) 
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
06-21 17:29:13.866 12640-12640/com.ccc.iot_demo W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
+0

添加異常堆棧跟蹤 – Mrityunjay

回答

2

由於使用了錯誤的連接字符串此問題。

而不是使用物聯網集線器連接字符串,你需要使用具有以下格式設備的連接字符串

HostName=[YOUR HUB NAME].azure-devices.net;DeviceId=[YOUR DEVICE ID];SharedAccessKey=rr****************************************4w= 

簡單地說,你可以從Device Explorer得到這個連接字符串。

+0

非常感謝@Rita Han -MSFT –

+0

@Jignesh Ansodariya此答案解決您的問題嗎? –

+0

是麗塔挑釁 –

相關問題