小编Ani*_*tal的帖子

使用AsyncTask的doInBackground()内的OkHttp库发出HTTP请求会阻止UI操作

我正在尝试使用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)

android okhttp

5
推荐指数
1
解决办法
5989
查看次数

将图像放在ImageView的左侧,以防ImageView占据整个屏幕宽度?

<ImageView
    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/nextarrow_final"
/>
Run Code Online (Sandbox Code Playgroud)

我希望这张图片在左侧.

任何帮助?

android

2
推荐指数
1
解决办法
4295
查看次数

是基于RESTup的基于REST的库的OkHttp吗?

找不到,是否早些时候回答过.

来自squareup,基于REST的OkHttp库是不是?请提供一些提及此的网页链接.

android

0
推荐指数
1
解决办法
49
查看次数

标签 统计

android ×3

okhttp ×1