使用okhttp 报Can't create handler inside thread that has not called Looper.prepare()

我是在用okhttp的请求数据,在处理数据的时候,打开了一个dialog用来提示,然后报了这个错误。

经过调试,发现错误原因是: dialog必须在一个被Looper.prepare()回调的的线程里创建,但是okhttp这个线程不具备这个条件

<span style="font-size:14px;">OkHttpUtil.getDataInGet(updateUrl, new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                    enterHome();
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    String result = response.body().string();
                    if (!TextUtils.isEmpty(result)) {
                        processData(result);
                    }
                }
            });</span>

在onResponse()线程进行下面操作。

<span style="font-size:14px;">private void showUpdateDialog(final UpdateInfo updateInfo) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("有新版本,快来更新吧");
        builder.setMessage(updateInfo.description);
        builder.setCancelable(false);
        builder.setPositiveButton("更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                downloadApk(updateInfo);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                enterHome();
            }
        });
        builder.show();
    }</span>

解决办法:

1.换一个线程来创建dialog,可以使用handler,

2.如果是activity,用runOnmainThread(),

我采用的是第二种方式解决的问题。

使用okhttp 报Can't create handler inside thread that has not called Looper.prepare()

时间: 2024-10-06 11:54:04

使用okhttp 报Can't create handler inside thread that has not called Looper.prepare()的相关文章

Can’t create handler inside thread that has not called Looper.prepare()

1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程内:而Android 4.0版本开始,Google强制要求这类操作必须在子线程内进行,否则将抛出 NetworkOnMainThreadException 异常. (2)操作UI必须只能在主线程内进行,否则报“Can’t create handler inside thread that has n

Android 线程更新UI报错 : Can&#39;t create handler inside thread that has not called Looper.prepare()

MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toString(); String timelength = lengthText.getText().toString(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new NewsService(getApp

关于子线程使用Toast报错Can&#39;t create handler inside thread that has not called Looper.prepare()的解决办法

形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherData = getWeatherData(strUrl); parseJson(weatherData); }catch(Exception e){ Toast.makeText(WindowApplication.getAppContext(), e.toString(), Toast.LENGT

Android handler 报错处理Can&#39;t create handler inside thread that has not called Looper.prepare()

解决方法,在ui线程里面创建handler m_MainActivity.runOnUiThread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub GameBoxUtil.startPay(m_MainActivity,String.valueOf(amount), productName, payCode,orderId,payHandler); }}); Android han

Android蓝牙开发,报BluetoothAdapter﹕ Can&#39;t create handler inside thread that has not called Looper.prepare

这个错误翻译的意思是:不能在没有Looper.prepare的线程里面创建handler. 起初我很疑惑,我根本没有用到工作线程,也没有创建handler.报错的代码如下: // Device scan callback. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final Blu

转 在子线程中new Handler报错--Can&#39;t create handler inside thread that has not called Looper.prepare()

在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has not called Looper.prepare()  这是因为Handler对象与其调用者在同一线程中,如果在Handler中设置了延时操作,则调用线程也会堵塞.每个Handler对象都会绑定一个Looper对象,每个Looper对象对应一个消息队列(MessageQueue).如果在创建Ha

Android : Can&#39;t create handler inside thread that has not called Looper.prepare()

又报错了,不过早也习以为常了. Can't create handler inside thread that has not called Looper.prepare() 我把文档给摘录下来了,大家可以看看. 这个类被用于为线程运行消息循环.默认线程并没有消息循环与之关联,所以你需要创建一个,在线程中调用prepare()以运行这个循环,然后调用loop()在循环结束时获取进程信息. 和消息循环交互最多的就是通过Handler类. 下面是一个实现了Looper线程的典型实例,通过分离的pre

Can&#39;t create handler inside thread that has not called Looper.prepare()

最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override public void handleMessage(android.os.Message msg) { super.handleMessage(msg); Bundle bundle = msg.getData(); isStop = bundle.getBoolean(mContext.getTex

解决使用Handler时Can&#39;t create handler inside thread that has not called Looper.prepare()

在android开发中,主线程不能进行耗时操作,所以我们经常把耗时操作放在子线程中进行,那么就需要子线程与主线程相互交流,就需要使用到handler. 而在使用handler过程中,如果对handler使用不太熟练的话就偶尔会出现Can't create handler inside thread that has not called Looper.prepare()的报错异常.之前在Handler的原理的博文中有讲到,Handler的使用会依靠Looper循环来发送消息,如果不创建Loope