Android SDK开发指南-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Android SDK开发指南

青衫无名 2017-10-23 15:33:25 2314


1.前言


本文旨在介绍MAC Android SDK的接入步骤和使用方法

2.安装



2.1 配置maven仓库


build.gradle添加阿里云maven仓库 allprojects {
    repositories {
        maven {
            url "http://maven.aliyun.com/nexus/content/repositories/releases"
        }
    }
}



2.2 配置gradle依赖dependencies {
    compile 'com.aliyun.ams:alicloud-android-mac:1.0.0'
}




目前MAC android sdk只支持arm架构,建议用真机进行测试

2.3 Manifest配置



2.3.1 添加组件<service
            android:name="anetwork.channel.aidl.NetworkService"
            android:exported="false">
            <intent-filter>
                <action android:name="anetwork.channel.aidl.IRemoteNetworkGetter" />
            </intent-filter>
        </service>





2.3.2 添加权限<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />





2.4 Proguard配置-keep class com.aliyun.ams.** {*;}
-keep public class org.android.spdy.** {*;}
-dontwarn com.alibaba.**
-dontwarn com.taobao.**
-dontwarn anetwork.channel.**
-dontwarn org.android.**





3.支持的版本


mac sdk支持的android最小版本为10 minSdkVersion 10


4.API

  • MacClient

MacClient主要用来发起请求Request和得到响应Response,使用方法请参考 最佳实践public final class MacClient {
    // 用于MAC sdk的初始化
    public static void init(MacConfig config);
    // 根据输入的Request获得一个Call对象
    public Call newCall(Request request);
    // MacClient的Builder
    public static final class Builder {
        public MacClient build();
    }
}

  • MacConfig

MAC sdk在初始化时需要传入全局配置MacConfig,使用方法请参考[url=https://help.aliyun.com/document_detail/27122.html?spm=5176.doc27121.6.565.kYBlFv#5.1 初始化]最佳实践-初始化[/url]: public final class MacConfig {
     // MacConfig的Builder
    public static final class Builder {
         // 设置Context
         public Builder context(Context context);
         // 设置appKey
         public Builder appKey(String appKey);
         // 设置appSecret
         public Builder appSecret(String appSecret);
         // 创建MacConfig对象
         public MacConfig build();
    }
}

  • Request

Request表示一个HTTP请求,每一个Request包含一个URL、method、请求header和body,使用方法请参考[url=https://help.aliyun.com/document_detail/27122.html?spm=5176.doc27121.6.565.kYBlFv#5.2 构建请求对象]最佳实践-构建请求对象[/url]: public final class Request {
     // 返回URL
    public String url();
    // 返回method,默认为Get
    public String method();
    // 返回请求头部
    public Map<String, String> headers();
    // 返回请求body
    public byte[] body();
    // Request的Builder
    public static final class Builder {
        // 设置URL
        public Builder url(String url);
        // 设置method
        public Builder method(String method, byte[] body);
        // 设置header
        public Builder headers(Map<String, String> headers);
        // 添加header
        public Builder addHeader(String name, String value);
        // 移除header
        public Builder removeHeader(String name);
        // 构建Request对象
        public Request build();
    }
}

  • Response

Response表示一个Request的响应,每一个Response包含状态码、响应头部以及响应body: public final class Response {
     // 返回状态码
    public int code();
    // 返回响应头部
    public Map<String, String> headers();
    // 返回响应body
    public byte[] body();
    // 返回请求是否成功
    public boolean isSuccessful();
}

  • Callback

MAC sdk允许用户使用异步Callback的方式,正常时返回Response,异常时返回MacException,使用方法请参考[url=https://help.aliyun.com/document_detail/27122.html?spm=5176.doc27121.6.565.kYBlFv#5.4 异步请求过程]最佳实践-异步请求过程[/url]: public interface Callback {
     // 正常时返回Response
    void onResponse(Call call, Response response);
    // 异常时返回MacException
    void onFailure(Call call, MacException exception);
}



5.最佳实践



5.1 初始化


第一步:调用MacConfig.init方法,设置AppKey,AppSecret,Context,建议在Application.onCreate时调用: public class DemoApplication extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化MacConfig
        MacConfig config = new MacConfig.Builder()
                .context(this)
                .appKey(APP_KEY)
                .appSecret(APP_SECRET)
                .build();
        MacClient.init(config);
    }
}


其中,AppKey和AppSecret可在 App列表页 获取。
第二步:构造MacClient对象,通过该对象来进行网络操作: // 构造MacClient对象
MacClient client = new MacClient.Builder().build();



5.2 构建请求对象


请求对象Request可以设置url,header,method等,其中method默认为Get方法: Request req = new Request.Builder()
                            .url(url)
                            .headers(headers)
                            .addHeader("User-Agent", "Your UA")
                            .method("POST", body)
                            .build();



5.3 同步请求过程


下面为移动加速的同步请求示例,[backcolor=transparent]使用时请确保同步请求方法在后台线程中执行: new Thread(new Runnable() {
    @Override
    public void run() {
        Response rsp = null;
        try {
            rsp = client.newCall(req).execute();
        } catch (MacException e) {
            e.printStackTrace();
        }
        if (rsp != null) {
            int statusCode = rsp.code();
            byte[] data = rsp.body();
            Log.d(TAG, "[DemoActivity] execute statusCode: " + statusCode + " data: " + new String(data));
        }
    }
}).start();



5.4 异步请求过程


下面为移动加速的异步请求示例,[backcolor=transparent]使用时请确保异步请求方法在后台线程中执行: new Thread(new Runnable() {
    @Override
    public void run() {
        client.newCall(req).enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                int statusCode = response.code();
                byte[] data = response.body();
                Log.d(TAG, "[DemoActivity] onResponse statusCode: " + statusCode + " data: " + new String(data));
            }
            @Override
            public void onFailure(Call call, MacException e) {
                Log.d(TAG, e.getMessage(), e);
            }
        });
    }
}).start();



如何判断加速是否成功


  • 过滤和查看tag为mac的日志,例如控制台通过adb logcat -s mac来过滤

  • 请求成功后可以看到类似日志:[DHandler] url: https://xxx/xxx.html AccSuccess: 1 reqSuccess: 1


其中,AccSuccess为1表示加速成功,reqSuccess为1表示请求成功
Java 开发工具 Maven Android开发
分享到
取消 提交回答
全部回答(0)
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程