計算のためAsyncTask
待機get()
方法はdoInBackground
方法で終了し、その結果を取得するためです。こちらを参照してくださいlink。 doInBackground
がその実行を終了したり、いくつかの例外が発生があるまで
これはあなたのメインのUIThread
でウェイトモードになります(すなわち。CancellationException
、ExecutionException
とInterruptedException
)。
onPostExecute(Result)
オーバーライド方法AsyncTask
を使用する必要があります。
private class OkHttpHandler extends AsyncTask<String, Void, byte[]> {
OkHttpClient client = new OkHttpClient();
@Override
protected byte[] doInBackground(String... params) {
Request.Builder builder = new Request.Builder();
builder.url(params[0]);
Request request = builder.build();
try {
Response response = client.newCall(request).execute();
return response.body().bytes();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] bytes) {
super.onPostExecute(bytes);
try {
if (bytes != null && bytes.length > 0) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageView.setImageBitmap(bitmap);
tv.setText("Total btytes download: " + bytes.length);
}
} catch (Exception e) {
tv.setText("sorry, something went wrong!");
}
}
}
どこでもThread.sleep()またはwait()を使用していません。 –
いくつかのコードを共有できますか? – M4R1KU
@ M4R1KU:ソースコードを参照してください。 –