[問題] 讀取API KEY的程式

作者: jerryxin (jerry)   2017-12-04 13:47:24
各位高手好
小弟最近在做APP
API接口要載入key
java範例是這樣的
public static String Signature(String xData, String AppKey) throws
java.security.SignatureException {
try {
final Base64.Encoder encoder = Base64.getEncoder();
// get an hmac_sha1 key from the raw key bytes
SecretKeySpec signingKey = new
SecretKeySpec(AppKey.getBytes("UTF-8"),"HmacSHA1");
// get an hmac_sha1 Mac instance and initialize with the signing
key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(xData.getBytes("UTF-8"));
String result = encoder.encodeToString(rawHmac);
return result;
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : "+
e.getMessage());
}
}
放到Android Studio上
1.一直抓不到Encoder這個method
是不是import 要改抓JAVA 的包才行
或者在Android上不是這樣寫的
小弟也看了Base64的方法有encode沒有encoder
2. throws java.security.SignatureException
這是JAVA的寫法 在Android上是不是直接刪掉
Android好像不用throws這種方法
作者: ssccg (23)   2017-12-04 15:29:00
android在API26才有java8的class(java.util.Base64)要用android.util.Base64怎麼用看javadoc不就很清楚了...Android沒有不用throw,exception處理都是一樣的

Links booklink

Contact Us: admin [ a t ] ucptt.com