关于Android设备动态注册的疑问
动态注册只是多了一步https请求换取三元组过程
productKey = productKeyView.getText().toString();
productSecret = productSecretView.getText().toString();
deviceName = deviceNameView.getText().toString();
try {
String url = 'https://iot-auth.cn-shanghai.aliyuncs.com/auth/register/device';
String random = String.valueOf(System.currentTimeMillis());
String signMethod = 'hmacsha1';
HashMap params = new HashMap<>();
params.put('productKey', productKey);
params.put('deviceName', deviceName);
params.put('random', random);
//签名
String sign = AliyunIoTSignUtil.sign(params, productSecret, signMethod);
//post的form参数
String form = 'productKey=' + productKey + '&deviceName=' + deviceName + '&random=' + random + '&sign=' + sign + '&signMethod=' + signMethod;
Log.d(TAG, 'formbody ' + form);
RequestBody body = RequestBody.create(MediaType.parse('application/x-www-form-urlencoded'), form);
final Request request = new Request.Builder()
.url(url)
.post(body)
.build();
final OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
responseBody = response.body().string();
Log.d(TAG, 'response body ' + responseBody);
JSONObject json = new JSONObject(responseBody);
deviceSecret = json.getJSONObject('data').getString('deviceSecret');
} catch (Exception e) {
e.printStackTrace();
}
赞0
踩0