API 通道 SDK

更新时间:2018-02-05 15:08:09

概述

API 通道 SDK 是 IoT 官方服务的 API 通道。 API 通道 基于 HTTP 协议,并通过整合安全组件来提升通道的安全性。

开通服务

本 SDK 涉及的功能依赖官方基础服务,需要先在控制台开启,方才可以使用。
如何开通服务,查看这里>

初始化

在初始化 SDK 前,需要正确的配置安全图片,请参见:集成安全图片 。然后参考下面的示例代码初始化 SDK:

        // 初始化无线保镖
        try {
            SecurityInit.Initialize(app);
        } catch (JAQException ex) {
            Log.e(TAG, "security-sdk-initialize-failed");
        } catch (Exception ex) {
            Log.e(TAG, "security-sdk-initialize-failed");
        }

        // 初始化 IoTAPIClient
        IoTAPIClientImpl.InitializeConfig config = new IoTAPIClientImpl.InitializeConfig();
        config.host = "official.api.feifengiot.com";
        config.apiEnv = Env.RELEASE;

        IoTAPIClientImpl impl = IoTAPIClientImpl.getInstance();
        impl.init(app, config);

安全图片未正确集成的时候,会导致安全组件初始化失败。常见的失败情形有以下三种:

ErrorCode=103
在logcat中可以看到类似如下的报错日志:

12-16 16:26:11.430 10486-10486/? W/System.err: ErrorCode = 103
12-16 16:26:11.430 10486-10486/? W/System.err: com.alibaba.wireless.security.open.SecException: java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.aliyun.iot.demo.wuxi.demoapp/app_SGLib/app_1513412765/libsgmainso-5.3.38.so" is 32-bit instead of 64-bit
12-16 16:26:11.430 10486-10486/? W/System.err:     at com.alibaba.wireless.security.mainplugin.SecurityGuardMainPlugin.onPluginLoaded(Unknown Source)
12-16 16:26:11.430 10486-10486/? W/System.err:     at com.alibaba.wireless.security.framework.e.a(Unknown Source)
12-16 16:26:11.430 10486-10486/? W/System.err:     at com.alibaba.wireless.security.framework.e.c(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.framework.e.c(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.open.initialize.b.a(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.open.initialize.a.loadLibrarySync(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.open.initialize.a.initialize(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.alibaba.wireless.security.jaq.SecurityInit.Initialize(Unknown Source)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.aliyun.iot.demo.wuxi.demoapp.DemoApplication.onCreate(DemoApplication.java:34)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1025)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5403)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread.-wrap2(ActivityThread.java)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.os.Looper.loop(Looper.java:154)
12-16 16:26:11.431 10486-10486/? W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6119)
12-16 16:26:11.431 10486-10486/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
12-16 16:26:11.431 10486-10486/? W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这种情形一般是 so 文件加载失败导致的,因为我们的 SDK 仅提供了 armeabi, x86 两种架构的so,所以,需要过滤其他类型的 so 文件,可以在 build.gradle 文件中添加如下配置:

android {
    compileSdkVersion 26
    defaultConfig {

        ...

        ndk {
            abiFilters "armeabi", "x86"//过滤除 armeabi 和 x86 以外的 so 文件
        }
    }
}

ErrorCode=202
在logcat中可以看到类似如下的报错日志:

12-16 13:19:06.586 9344-9344/com.aliyun.iot.demo W/System.err: ErrorCode = 202
12-16 13:19:06.587 9344-9344/com.aliyun.iot.demo W/System.err: com.alibaba.wireless.security.open.SecException: 
12-16 13:19:06.594 9344-9344/com.aliyun.iot.demo W/System.err:     at com.taobao.wireless.security.adapter.JNICLibrary.doCommandNative(Native Method)
12-16 13:19:06.595 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.wireless.security.mainplugin.a.doCommand(Unknown Source:0)
12-16 13:19:06.595 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.wireless.security.a.j.a.getAppKeyByIndex(Unknown Source:21)
12-16 13:19:06.596 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.getAppKey(Unknown Source:7)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister.h(Unknown Source:17)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister.a(Unknown Source:34)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.impl.j.a(Unknown Source:30)
12-16 13:19:06.597 9344-9344/com.aliyun.iot.demo W/System.err:     at com.alibaba.sdk.android.push.impl.j.register(Unknown Source:1)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at com.aliyun.iot.aep.demo.DemoApplication.initPush(DemoApplication.java:115)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at com.aliyun.iot.aep.demo.DemoApplication.onCreate(DemoApplication.java:27)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1118)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5791)
12-16 13:19:06.599 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread.-wrap1(Unknown Source:0)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.os.Looper.loop(Looper.java:164)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6541)
12-16 13:19:06.601 9344-9344/com.aliyun.iot.demo W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-16 13:19:06.604 9344-9344/com.aliyun.iot.demo W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
12-16 13:19:06.604 9344-9344/com.aliyun.iot.demo W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

这种情形一般是安全图片与当前 apk 的签名不匹配导致的,可以考虑修改当前 apk 签名或者重新上传 apk 生成新的安全图片

ErrorCode=203
在logcat中可以看到类似如下的报错日志:

12-16 13:29:59.005 10232-10267/? W/System.err: ErrorCode = 203
12-16 13:29:59.005 10232-10267/? W/System.err: com.alibaba.wireless.security.open.SecException: 
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.taobao.wireless.security.adapter.JNICLibrary.doCommandNative(Native Method)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.wireless.security.mainplugin.a.doCommand(Unknown Source:0)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.wireless.security.a.j.a.getAppKeyByIndex(Unknown Source:21)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.getAppKey(Unknown Source:7)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.a(Unknown Source:8)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.securitybox.alipush.a.getMpsDeviceId(Unknown Source:2)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister$a.b(Unknown Source:68)
12-16 13:29:59.005 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.vip.AppRegister$a.a(Unknown Source:0)
12-16 13:29:59.006 10232-10267/? W/System.err:     at com.alibaba.sdk.android.push.vip.b.handleMessage(Unknown Source:54)
12-16 13:29:59.006 10232-10267/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
12-16 13:29:59.006 10232-10267/? W/System.err:     at android.os.Looper.loop(Looper.java:164)
12-16 13:29:59.006 10232-10267/? W/System.err:     at android.os.HandlerThread.run(HandlerThread.java:65)

这种情形是因为没找到安全图片导致的。请注意检查安全图片是否在 drawable 目录中,或者检查安全图片的名称的末四位和当前 authCode 是否一致(authCode 默认为 114d)。

关于 IoTAPIClientImpl 的说明,请参见:IoTAPIClientImpl;

使用方式

请参考如下示例,使用 SDK:


        // 构建请求
        IoTRequest request = new IoTRequestBuilder()
                .setScheme(scheme) // 如果是HTTPS,可以省略本设置
                .setHost(host) // 如果是IoT官方服务,可以省略本设置
                .setPath("/kit/debug/ping") // 参考业务API文档,设置path
                .setApiVersion("1.0.0")  // 参考业务API文档,设置apiVersion
                .addParam("input", "测试") // 参考业务API文档,设置params,也可使用setParams(Map<Strign,Object> params)
                .build();

        // 获取Client实例,并发送请求
        IoTAPIClient ioTAPIClient = new IoTAPIClientFactory().getClient();
        ioTAPIClient.send(request, new IoTCallback() {
            @Override
            public void onFailure(IoTRequest request, Exception e) {
                // 根据 e, 处理异常
            }

            @Override
            public void onResponse(IoTRequest request, IoTResponse response) {
                int code = response.getCode();

                // 200 代表成功
                if(200 != code){
                    String mesage = response.getMesasge();
                    String localizedMsg = response.getLocalizedMsg();
                    // 根据 mesage 和 localizedMsg,处理失败信息
                    ...

                    return;
                }

                Object data = response.getData();
                // 解析 data
                ...
            }
        });

混淆配置


-keep public class com.aliyun.iot.aep.sdk.apiclient.** {
    public <methods>;
    public <fields>;
}

更多功能

添加日志

API 通道 SDK 本身没有输出任何请求日志(从性能和安全性的角度考虑)。如果开发者在开发过程中,需要添加相关日志,可以自行添加Tracker,打印相关日志。详情可以参考 DemoApp 源码 和IoTAPICLientImpl.setPerformanceTracker()

results matching ""

    No results matching ""