我正在实现播放完整性API,并在每个监听器中,我想处理任何异常。
这是传递空nonce时发生的示例:
IntegrityManager integrityManager = IntegrityManagerFactory.create(getApplicationContext());
Task<IntegrityTokenResponse> integrityTokenResponse = integrityManager.requestIntegrityToken(IntegrityTokenRequest.builder().setNonce("").build());
integrityTokenResponse
.addOnCompleteListener(new OnCompleteListener<IntegrityTokenResponse>() {
@Override
public void onComplete(@NonNull Task<IntegrityTokenResponse> task) {
Log.d(LOG_TAG, "completed");
if(!task.isSuccessful()) {
Log.d(LOG_TAG, "!isSuccessful");
Exception e = task.getException();
Log.d(LOG_TAG, e.getMessage());
}
}
}
);
输出:
完成!isSuccessful
-10: Integrity API error (-10): Nonce length too short。nonce必须至少为16字节(在base64编码之前),以获得更好的安全性。请使用更长的nonce重试。
(https://developer.android.com/google/play/integrity/reference/com/google/android/play/core/integrity/model/IntegrityErrorCode.html # NONCE_TOO_SHORT)。
失败-10: Integrity API error (-10): Nonce length too short。nonce必须至少为16字节(在base64编码之前),以获得更好的安全性。请使用更长的nonce重试。
(https://developer.android.com/google/play/integrity/reference/com/google/android/play/core/integrity/model/IntegrityErrorCode.html # NONCE_TOO_SHORT)。
在异常urlNONCE_TOO_SHORT
对应于异常消息中显示的-10
。
官方文档解释了错误代码,但是如何从异常/结果中获得这些代码?(我更喜欢一种不从异常消息的文本中提取它的方式)
我已经寻找了一些异常方法,如get*Code
或getError
或类似的,也在侦听器任务变量上,但它们不是。
你可以在
里面添加下面的代码令牌请求的.addOnFailureListener { }
val errorMessage = getErrorText(it as IntegrityServiceException)
让它像这样
.addOnFailureListener {val errorMessage = getErrorText(it as IntegrityServiceException) }
和getErrorText()将实现如下
private fun getErrorText(it: IntegrityServiceException): String {
when (it.errorCode) {
IntegrityErrorCode.API_NOT_AVAILABLE -> return ""
IntegrityErrorCode.PLAY_STORE_NOT_FOUND -> return ""
IntegrityErrorCode.NETWORK_ERROR -> return ""
IntegrityErrorCode.PLAY_STORE_ACCOUNT_NOT_FOUND -> return ""
IntegrityErrorCode.APP_NOT_INSTALLED -> return ""
IntegrityErrorCode.PLAY_SERVICES_NOT_FOUND -> return ""
IntegrityErrorCode.APP_UID_MISMATCH -> return ""
IntegrityErrorCode.TOO_MANY_REQUESTS -> return ""
IntegrityErrorCode.CANNOT_BIND_TO_SERVICE -> return ""
IntegrityErrorCode.NONCE_TOO_SHORT -> return ""
IntegrityErrorCode.NONCE_TOO_LONG -> return ""
IntegrityErrorCode.GOOGLE_SERVER_UNAVAILABLE -> return ""
IntegrityErrorCode.NONCE_IS_NOT_BASE64 -> return ""
IntegrityErrorCode.PLAY_STORE_VERSION_OUTDATED -> return ""
IntegrityErrorCode.PLAY_SERVICES_VERSION_OUTDATED -> return ""
IntegrityErrorCode.CLOUD_PROJECT_NUMBER_IS_INVALID -> return ""
IntegrityErrorCode.CLIENT_TRANSIENT_ERROR -> return ""
IntegrityErrorCode.INTERNAL_ERROR -> return ""
}
return ""
}
所以最终代码看起来像下面的
IntegrityManagerFactory.create(applicationContext).requestIntegrityToken(
IntegrityTokenRequest.builder().setNonce(
Base64.encodeToString(
requestNonce, Base64.URL_SAFE or Base64.NO_WRAP
)
).build()
).addOnSuccessListener { response1 ->
//localWay(response, decodedDecryptionKey,decodedVerificationKey)
//cloudWay(response1)
}.addOnFailureListener {
val errorMessage = getErrorText(it as IntegrityServiceException)
}