开发实例

更新时间:2018-07-31 16:29:07

1.阿里云工业互联网平台开放API的SDK使用说明</span>

登录阿里云工业互联网平台,左侧导航栏点开【服务调用】菜单,点击【SDK使用说明</span>】,即可获取Java,Python ,PHP,.NET语言调用示例。

2.阿里云工业互联网平台开放API的请求结构

阿里云工业互联网平台开放API在阿里云API网关基础上增加了request和params节点。

  • request包含iotToken和apiVer(工业平台api版本)

  • params包含工业平台api的入参列表

image.png | left | 472x316

完整API网关请求的Json示例如下:

{
   id: UUID.v1(), //API网关 请求唯一标识,必填
   version: '1.0', //API网关协议版本,固定值1.0

   request: {     //工业平台参数
      apiVer:'1.0.3'  //工业平台api版本,必填
   },
   params: {
      key1:'value1', //工业平台api的入参
      key2:'value2' , //工业平台api的入参
   }
}

2.完整调用示例

Java语言:

2.1 引入API网关sdk

<dependency>
  <groupId>com.aliyun.api.gateway</groupId>
  <artifactId>sdk-core-java</artifactId>
  <version>1.0.4</version> <!-- 替换为最新版本 -->
</dependency>

2.2 根据API定义,构造参数,发起https请求,获取返回数据。

import com.alibaba.cloudapi.sdk.core.BaseApiClient;
import com.alibaba.cloudapi.sdk.core.enums.Method;
import com.alibaba.cloudapi.sdk.core.enums.Scheme;
import com.alibaba.cloudapi.sdk.core.http.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.core.model.ApiRequest;
import com.alibaba.cloudapi.sdk.core.model.ApiResponse;
import com.alibaba.cloudapi.sdk.core.model.BuilderParams;
import com.alibaba.fastjson.JSONObject;

import java.io.UnsupportedEncodingException;

public class IIoTAPIGatewayDemo extends BaseApiClient {

    public IIoTAPIGatewayDemo(BuilderParams builderParams) {
        super(builderParams);
    }

    public static void main(String [] args) throws UnsupportedEncodingException {

        BuilderParams clientParams = new BuilderParams();
        clientParams.setAppKey("你的AppKey");
        clientParams.setAppSecret("你的appSecret");
        //1.获取client实例
        IIoTAPIGatewayDemo client = new IIoTAPIGatewayDemo(clientParams);
        //2.构造阿里云平台开放API的Request结构体
        ApiRequest request = buildRequest();
        //3.发起工业开放API调用
        ApiResponse response = client.syncInvoke(request);

        System.out.println(response.getMessage());
        System.out.println(new String(response.getBody()));
    }
    /**
     * 构造请求body
     */
    private static ApiRequest buildRequest() throws UnsupportedEncodingException {
        String host = "api.link.aliyun.com";
        String path = "api的path";

        JSONObject request = new JSONObject();
        request.put("apiVer","1.0.3");//api版本,必填

        JSONObject params = new JSONObject();
        params.put("key1","value1");// api入参
        params.put("key2","value2");// api入参


        JSONObject requestBody = new JSONObject();
        requestBody.put("id",System.currentTimeMillis());//请求唯一标识,必填
        requestBody.put("version","1.0");//协议版本,固定值1.0
        requestBody.put("request",request);//请求唯一标识,必填
        requestBody.put("params",params);//api入参结构体


        System.out.println(requestBody.toJSONString());

        byte[] body = requestBody.toJSONString().getBytes("UTF-8");

        ApiRequest apiRequest = new ApiRequest(Scheme.HTTPS, Method.POST_BODY,
                host,path,
                body);
        apiRequest.getHeaders().put("accept", "application/json");

        return apiRequest;
    }

}

//查询设备详情完整例子
public class IIoTAPIGatewayDemo extends BaseApiClient {

    public IIoTAPIGatewayDemo(BuilderParams builderParams) {
        super(builderParams);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        BuilderParams clientParams = new BuilderParams();
        clientParams.setAppKey("24915xxx");
        clientParams.setAppSecret("a7a53ed132c7be05xxxxxxx");
        //1.获取client实例
        IIoTAPIGatewayDemo client = new IIoTAPIGatewayDemo(clientParams);
        //2.构造飞象平台开放API的Request结构体
        ApiRequest request = buildRequest();
        //3.发起工业开放API调用
        ApiResponse response = client.syncInvoke(request);

        System.out.println(response.getMessage());
        System.out.println(new String(response.getBody()));
    }

    /**
     * 构造请求body
     */
    private static ApiRequest buildRequest() throws UnsupportedEncodingException {
        String host = "api.link.aliyun.com";
        String path = "/thing/device/detail/get";

        JSONObject request = new JSONObject();
        //api版本,必填
        request.put("apiVer", "1.1.1");

        JSONObject params = new JSONObject();
        // api入参
        params.put("productKey", "b1Q03Kb1Yyg");
        // api入参
        params.put("deviceName", "n7m1HbDdinIcXCYh0Gkz");


        JSONObject requestBody = new JSONObject();
        //请求唯一标识,必填
        requestBody.put("id", System.currentTimeMillis());
        //协议版本,固定值1.0
        requestBody.put("version", "1.0");
        //请求唯一标识,必填
        requestBody.put("request", request);
        //api入参结构体
        requestBody.put("params", params);


        System.out.println(requestBody.toJSONString());

        byte[] body = requestBody.toJSONString().getBytes("UTF-8");

        ApiRequest apiRequest = new ApiRequest(Scheme.HTTPS, Method.POST_BODY, host, path, body);
        apiRequest.getHeaders().put("accept", "application/json");

        return apiRequest;
    }

}

results matching ""

    No results matching ""