Step By Step
- OpenAPI
- 云端SDK
一.OpenAPI
二.云端SDK(本文以Java为例)
1.添加pom依赖
<!--https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot --><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-iot</artifactId><version>7.31.0</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.6</version></dependency>
2.发起调用
StringaccessKey="${accessKey}"; StringaccessSecret="${accessSecret}"; IClientProfileprofile=DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret); DefaultAcsClientclient=newDefaultAcsClient(profile); PubRequestrequest=newPubRequest(); request.setIotInstanceId("${iotInstanceId}"); request.setProductKey("${productKey}"); request.setMessageContent(Base64.encodeBase64String("hello world".getBytes())); request.setTopicFullName("/${productKey}/${deviceName}/user/get"); request.setQos(0); //目前支持QoS0和QoS1。 try{ PubResponseresponse=client.getAcsResponse(request); System.out.println(response.getSuccess()); System.out.println(response.getCode()); System.out.println(response.getErrorMessage()); } catch (ServerExceptione) { e.printStackTrace(); } catch (ClientExceptione) { System.out.println("ErrCode:"+e.getErrCode()); System.out.println("ErrMsg:"+e.getErrMsg()); e.printStackTrace(); }