我正在尝试使用call.execute() - 同步调用在AsyncTask中发出OkHttp请求.
我的布局中有两个按钮.按下按钮1启动AsyncTask,执行OkHttp request.call.execute().
按下button2,我只更新一个TextView.
观察:当AsyncTask运行时,我无法更新TextView.
但是,如果我不使用AsyncTask并使用OkHttpClient.newCall().enqueue()方法,那么我可以通过按button2来更新TextView.
对于"为什么在这种情况下使用AsyncTask不起作用"的任何答案?
源代码示例:
bpost = (Button) findViewById(R.id.bpost);
bpost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
tv.setText(""+i);
}
});
bstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OkHttpHandler handler = new OkHttpHandler();
byte[] image = new byte[0];
try {
image = handler.execute(url).get();
if (image != null && image.length > 0) {
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
imageView.setImageBitmap(bitmap);
tv.setText("Total btytes download: " + image.length);
}
} catch (Exception e) { …Run Code Online (Sandbox Code Playgroud) <ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/nextarrow_final"
/>
Run Code Online (Sandbox Code Playgroud)
我希望这张图片在左侧.
任何帮助?
找不到,是否早些时候回答过.
来自squareup,基于REST的OkHttp库是不是?请提供一些提及此的网页链接.