Personal blog
www.milovetingting.cn
preface
Simply record the process of integrating aliyun HTTPDNS service
What is a HTTPDNS
HTTPDNS is a domain name resolution service for multiple applications (mobile apps and PC clients). It features anti-hijacking, precise scheduling, and real-time resolution.
The above is from aliyun HTTPDNS document.
Access to the process
The service
HTTPDNS is a service provided by the mobile development platform. Please refer to EMAS Quick Start service.
- After opening the service, enter the console and click Add Product, as shown in the figure
-
After the product is added, click Add Application, as shown in the picture
-
After adding the app, click the app you just added
-
Check the Account ID, which will be used later to configure DNS
-
Add a domain name to the domain name list
Access to the Android SDK,
- Add the Maven repository address to the build.gradle file in the project root directory
allprojects {
repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/releases/'}}}Copy the code
- Add dependencies in build.gradle under the module that needs to import the HTTPDNS service
dependencies {
compile ('com. Aliyun. Ams: alicloud - android - HTTPDNS: 1.3.3 @ aar') {
transitive true}}Copy the code
An error message is displayed after the project is synchronized according to the previous official documents
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-andro Id - 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 Required - https://jcenter.bintray.com/com/aliyun/ams/alicloud-android-utdid/1.1.5.4/alicloud-android-utdid-1.1.5.4.pom Ams: alicloud-Android-httpdns :1.3.3 > com.aliyun.ams: alicloud-Android-beacon :1.0.4.3Copy the code
Modify the following
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'
Copy the code
-
Retrofit and Gson were introduced
Increased reliance on
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' Copy the code
-
Configure DNS
Create a new class that inherits Dns
public class AliDns implements Dns { private HttpDnsService httpDns; public AliDns(Context context) { // Pass in account_id, which is generated when an application is added to the HTTPDNS console httpDns = HttpDns.getService(context, "account_id"); } @Override public List<InetAddress> lookup(String hostname) throws UnknownHostException { // Obtain the IP address through the asynchronous parsing interface String ip = httpDns.getIpByHostAsync(hostname); / / Android9.0 system and later, HTTPS requests can't directly access, convenience, directly in the AndroidManifest. XML configuration android: usesCleartextTraffic = "true" if(ip ! =null) { // If the IP address is not null, the network request is directly made using the IP address Log.e("AliDns"."ip:" + ip); List<InetAddress> inetAddresses = Arrays.asList(InetAddress.getAllByName(ip)); return inetAddresses; } // If null is returned, the system DNS resolves the domain name returnDns.SYSTEM.lookup(hostname); }}Copy the code
Set the DNS for OkHttpClient
OkHttpClient client = new OkHttpClient.Builder().dns(new AliDns(getApplicationContext())).build(); Copy the code
-
Call request
Retrofit retrofit = new Retrofit.Builder().client(client).baseUrl("Domain name").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("Request failed, error code :" + response.code()); return; } Bean bean = response.body(); tv.setText(bean.toString()); } @Override public void onFailure(Call<Bean> call, Throwable t) { tv.setText(t.getMessage()); }});Copy the code
The source code
Github.com/milovetingt…