我正在尝试创建一个基本的 AsyncTask 以从密钥库中获取证书。
出于某种原因,即使是最简单的 AsyncTask 也不适合我,我不断收到有关 ExceptionInIntilizationError 的错误。
这是我用来让基本异步任务工作的测试代码:
public class Authenticator extends Activity {
PrivateKey privateKey = null;
String SavedAlias = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getCertificates("TEST");
doSomething();
}
public void doSomething()
{
new additionalStuff().execute();
}
public class AliasLoader extends AsyncTask<Void, Void, X509Certificate[]>
{
X509Certificate[] chain = null;
@Override protected X509Certificate[] doInBackground(Void... params) {
android.os.Debug.waitForDebugger();
if(!SavedAlias.isEmpty())
{
try {
chain = KeyChain.getCertificateChain(getApplicationContext(),SavedAlias);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
else
{
this.cancel(true);
}
return chain;
}
@Override protected void onPostExecute(X509Certificate[] chain)
{
try {
privateKey = KeyChain.getPrivateKey(getApplicationContext(), SavedAlias);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
if (privateKey != null) {
Signature signature = null;
try {
signature = Signature.getInstance("SHA1withRSA");
} catch (NoSuchAlgorithmException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
try {
signature.initSign(privateKey);
} catch (InvalidKeyException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
public void getCertificates(String Host)
{
KeyChainAliasCallback callBack = new KeyChainAliasCallback() {
@Override
public void alias(String alias) {
if (alias != null)
{
saveAlias(alias);
//doSomething();
}
}
};
KeyChain.choosePrivateKeyAlias(this, callBack,
new String[] {"RSA", "DSA"}, // List of acceptable key types. null for any
null, // issuer, null for any
null, // host name of server requesting the cert, null if unavailable
443, // port of server requesting the cert, -1 if unavailable
null); // alias to preselect, null if unavailable
}
public void saveAlias(String alias)
{
SavedAlias = alias;
}
public class additionalStuff extends AsyncTask<String, String, Void>
{
String test = "This is a Test!!!";
@Override
protected void onPreExecute()
{
}
@Override
protected Void doInBackground(String... params) {
return null;
}
@Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void unused)
{
Log.d("DEEPAM", "THIS IS A TEST");
return;
}
}
}
为什么会失败?=(
** 更新 ** 我进行了更改以在执行后添加 @Override 和参数 Void Void ,它仍然出现相同的 ExceptionInInitilizationError =(
**更新**
我猜我无法在 UI 中使用 Asynctask 创建一个新线程,这可能是它之前失败的原因,无论如何我可以在用户选择证书后调用 AsyncTask 吗?