Im trying to create a basic AsyncTask to get a certificate from the key store.
for some reason even the most simple AsyncTask doesnt work for me, i keep getting an error about ExceptionInIntilizationError.
This is the test code im using to just get a basic asynctask working:
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;
}
}
}
Why does this fail? =(
** UPDATE **
I made the changes to add the @Override on the postexcecution and the parameters Void Void and it still comes up with the same ExceptionInInitilizationError =(
** UPDADE **
Im guessing i cannot create a new thread using Asynctask in the UI, which is probably why it was failing before, is there anyway i can get the AsyncTask to be called once the user has selected a certificate?
I Figured out the problem, whatappserv, Vladimir and n_benbourahla mentioned probably would have been problems later on once my initial mistake had been fixed.
essentially i forgot to add in a bit of code that is using the execution.
I am calling the doSomething.execute() in the callback of a KeyChain.choosePrivateKeyAlias.
which it looks like putting it outside that and the asynctask works fine.
I still have the problem of how can i call the asynctask inside the callback from the keychain.choosePrivateAlias as it is essential that it is only run once that has executed. I will update the code with what i now have.