<以RegisterDevice创建设备接口为例>
1、通过Common sdk调用
pom.xml
JavaDemo
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
public class RegisterDevice {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("iot.cn-shanghai.aliyuncs.com");
request.setSysVersion("2018-01-20");
request.setSysAction("RegisterDevice");
request.putQueryParameter("RegionId", "cn-shanghai");
request.putQueryParameter("ProductKey", "a1dkPFDXTA4");
request.putQueryParameter("DeviceName", "lebron");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
测试结果
控制台上显示创建成功
2、使用云端sdk调用
pom.xml
public static RegisterDeviceResponse.Data registerDevice(String ProductKey, String DeviceName) {
RegisterDeviceResponse response = null;
RegisterDeviceRequest request = new RegisterDeviceRequest();
request.setDeviceName(DeviceName);
request.setProductKey(ProductKey);
try {
response = client.getAcsResponse(request);
if (response.getSuccess() != null && response.getSuccess()) {
LogUtil.print("注册设备成功");
LogUtil.print(JSON.toJSONString(response));
} else {
LogUtil.print("注册设备失败");
LogUtil.error(JSON.toJSONString(response));
}
return response.getData();
} catch (ClientException e) {
e.printStackTrace();
LogUtil.error("注册设备失败!" + JSON.toJSONString(response.getData()));
}
return null;
}
测试结果
控制台创建结果