私のMainActivityには、次のようなクラスの初期化があります。
Presentation presentation = new Presentation("link");
Presentationは、Webサーバー上の.JSONファイルの値を使用して初期化されるクラスです。
public Presentation(String URL) {
// Do stuff
doNetworking();
}
private doNetworking() {
// Network access here
// This throws the Network on Main Thread exception
}
MainActivityでは、次のステップでPresentationのすべての値が存在する必要があります。
Presentation presentation = new Presentation();
// Do some stuff with it
AsyncTaskを使用して、これをどのように行うべきかわかりません。これまでのところ、次のようなものがあります。public Presentation(String URL){//何か新しいInitializePresentation()。execute(URL); }
private class InitializePresentation extends AsyncTask<String, Void, Boolean> {
// Amongst other things
protected Boolean doInBackground(String... params) {
// Do the networking stuff here
}
}
私が必要としているのは、このコードをリファクタリングして、非同期であるが同期呼び出しのように動作するようにすることです。どんな助けでも大歓迎です。
編集 これを達成するためにコードをリファクタリングするにはどうすればよいですか?
Bitmap b = new Bitmap();
Load bitmap from network;
Use bitmap in imageview;
これはこのように使用できますか?または私はそれを次のように使用する必要がありますか
Async, doInBackground() {
Load bitmap from network
Use bitmap in imageview
Continue with application
}
ありがとうございました!