android iot收发短信,Android app开发对接华为平台总结——附源码

即:JAVA SDK无法在android 平台使用,如何在android平台开发app对接OC平台

android  app开发采用的是java语言,华为关于java开发提供了一个Demo和sdk,

但是在android平台中无法使用这个SDK进行开发,原因有二:

1、SDK中的证书格式是jks,android平台不支持;在android中支持的证书格式是bks;

2、SDK中使用的是apache的http框架,该框架在android 6.0中已经废除;无法继续使用;

因此,app的开发思路如下:

参考JAVA北向Demo,将httpclient部分修改成适配android平台的,推荐使用okhttp或者httpurlconnection;

现提供基于okhttp的连接实现,给出基于okhttp的https初始化即鉴权接口,供大家参考:

package com.huawei.utils;

import android.util.Log;

import java.io.IOException;

import java.security.KeyStore;

import java.util.HashMap;

import java.util.Map;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSocketFactory;

import javax.net.ssl.TrustManager;

import javax.net.ssl.TrustManagerFactory;

import javax.net.ssl.X509TrustManager;

import okhttp3.FormBody;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.Response;

public class HttpsUtil {

public final static String HTTPGET = "GET";

public final static String HTTPPUT = "PUT";

public final static String HTTPPOST = "POST";

public final static String HTTPDELETE = "DELETE";

public final static String HTTPACCEPT = "Accept";

public final static String CONTENT_LENGTH = "Content-Length";

public final static String CHARSET_UTF8 = "UTF-8";

private static OkHttpClient httpClient = null;

/**

* Two-Way Authentication In the two-way authentication, the client needs: 1

* Import your own certificate for server verification; 2 Import the CA

* certificate of the server, and use the CA certificate to verify the

* certificate sent by the server; 3 Set the domain name to not verify

* (Non-commercial IoT platform, no use domain name access.)

* */

public void initSSLConfigForTwoWay() throws Exception {

// 1 Import your own certificate

KeyStore selfCert = KeyStore.getInstance("pkcs12");

selfCert.load(Constant.SelfCertIn, Constant.SELFCERTPWD.toCharArray());

KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");

kmf.init(selfCert, Constant.SELFCERTPWD.toCharArray());

// 2 Import the CA certificate of the server,

KeyStore caCert = KeyStore.getInstance("BKS");

caCert.load(Constant.TrustCAIn, Constant.TRUSTCAPWD.toCharArray());

TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");

tmf.init(caCert);

//llb add for ignor CA verify

X509TrustManager truseAllManager = new X509TrustManager() {

@Override

public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)

throws java.security.cert.CertificateException {

}

@Override

public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)

throws java.security.cert.CertificateException  {

}

@Override

public java.security.cert.X509Certificate[] getAcceptedIssuers() {

return new java.security.cert.X509Certificate[0];

//return null

}

};

//llb end

SSLContext sc = SSLContext.getInstance("TLS");

sc.init(kmf.getKeyManagers(), new TrustManager[] { truseAllManager }, null);

SSLSocketFactory sslSock = sc.getSocketFactory();

// 3 Set the domain name to not verify

try {

httpClient = new OkHttpClient.Builder()

.hostnameVerifier(new DefaultHostnameVerifier())

.sslSocketFactory(sslSock, truseAllManager)

.build();

} catch (Exception e) {

Log.e("okhttp init err:",e.toString());

}

}

//鉴权接口

public void Login(String httpsUrl){

FormBody formBody = new FormBody.Builder()

.add("appId",Constant.APPID)

.add("secret",Constant.SECRET)

.build();

Request request = new Request.Builder().url(httpsUrl).post(formBody).build();

new Thread(new Runnable() {

@Override

public void run() {

try {

Response response = httpClient.newCall(request).execute();

String resLogin = response.body().string();

if (response.isSuccessful()) {

Log.i("LogInRsp",resLogin);

} else {

throw new IOException("Unexpected code " + response);

}

Map data = new HashMap<>();

data = JsonUtil.jsonString2SimpleObj(resLogin, data.getClass());

String accessToken = data.get("accessToken");

Log.i("Token",accessToken);

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

}

}

由于app刚开始编写,待完善后再开源!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值