个人博客
前言
简单记录集成阿里云HTTPDNS服务的过程
什么是HTTPDNS
HTTPDNS是面向多端应用(移动端APP,PC客户端应用)的域名解析服务,具有域名防劫持、精准调度、实时解析生效的特性。
以上来自阿里云HTTPDNS文档。
接入流程
服务开通
HTTPDNS是移动研发平台提供的服务,请参考EMAS 快速入门开通服务。
- 开通服务后,进入控制台,点击添加产品,如图所示
-
完成添加产品后,点击添加应用,如图所示
-
添加应用完成后,点击刚才添加的应用
-
查看Account ID,这个id后面配置dns会用到
-
在域名列表中添加域名
Android SDK 接入
- 在项目根目录下的build.gradle文件中添加Maven仓库地址
allprojects {
repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'
}
}
}
- 在需要引入HTTPDNS服务的模块下的build.gradle中添加依赖
dependencies {
compile ('com.aliyun.ams:alicloud-android-httpdns:1.3.3@aar') {
transitive true
}
}
按照以上官方文档配置,在同步工程后,会报错
Could not find com.aliyun.ams:alicloud-android-utdid:1.1.5.4.
Searched in the following locations:
- http://maven.aliyun.com/nexus/content/repositories/releases/com/aliyun/ams/alicloud-android-utdid/1.1.5.4/alicloud-android-utdid-1.1.5.4.pom
- https://dl.google.com/dl/android/maven2/com/aliyun/ams/alicloud-android-utdid/1.1.5.4/alicloud-android-utdid-1.1.5.4.pom
- https://jcenter.bintray.com/com/aliyun/ams/alicloud-android-utdid/1.1.5.4/alicloud-android-utdid-1.1.5.4.pom
Required by:
project :app > com.aliyun.ams:alicloud-android-httpdns:1.3.3 > com.aliyun.ams:alicloud-android-beacon:1.0.4.3
修改如下
implementation('com.aliyun.ams:alicloud-android-httpdns:1.3.3@aar') {
transitive true
exclude group: 'com.aliyun.ams', module: 'alicloud-android-utdid'
}
implementation 'com.aliyun.ams:alicloud-android-utdid:1.1.5.3'
-
引入Retrofit及gson
增加依赖
implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'com.squareup.retrofit2:converter-gson:2.3.0' implementation 'com.google.code.gson:gson:2.8.6'
-
配置DNS
新建一个类,继承Dns
public class AliDns implements Dns { private HttpDnsService httpDns; public AliDns(Context context) { //传入account_id,account_id为HTTPDNS控制台添加应用时生成的 httpDns = HttpDns.getService(context, "account_id"); } @Override public List<InetAddress> lookup(String hostname) throws UnknownHostException { //通过异步解析接口获取ip String ip = httpDns.getIpByHostAsync(hostname); //Android9.0系统及以后版本,https请求无法直接访问,方便起见,直接在AndroidManifest.xml中配置android:usesCleartextTraffic="true" if (ip != null) { //如果ip不为null,直接使用该ip进行网络请求 Log.e("AliDns", "ip:" + ip); List<InetAddress> inetAddresses = Arrays.asList(InetAddress.getAllByName(ip)); return inetAddresses; } //如果返回null,走系统DNS服务解析域名 return Dns.SYSTEM.lookup(hostname); } }
设置OkHttpClient的dns
OkHttpClient client = new OkHttpClient.Builder().dns(new AliDns(getApplicationContext())).build();
-
调用请求
Retrofit retrofit = new Retrofit.Builder().client(client).baseUrl("域名").addConverterFactory(GsonConverterFactory.create()).build(); Api api = retrofit.create(Api.class); Call<Bean> call = api.getBanner(); call.enqueue(new Callback<Bean>() { @Override public void onResponse(Call<Bean> call, Response<Bean> response) { if (!response.isSuccessful()) { tv.setText("请求失败,错误码:" + response.code()); return; } Bean bean = response.body(); tv.setText(bean.toString()); } @Override public void onFailure(Call<Bean> call, Throwable t) { tv.setText(t.getMessage()); } });