猿问

android 使用了runOnUiThread 还是报错NetworkOnMainThreadException,代码如下:

private EaseUser getUserInfo(final String username){
        // To get instance of EaseUser, here we get it from the user list in memory
        // You'd better cache it if you get it from your server
        String curUser = EMClient.getInstance().getCurrentUser();
        if(username==null||curUser==null)return null;
        EaseUser user = null;
    /*    if(username.equals(curUser))
            return getUserProfileManager().getCurrentUserInfo();*/
        user = getContactList().get(username);
        if(user == null && getRobotList() != null){
            user = getRobotList().get(username);
        }

        // if user is not in your contacts, set inital letter for him/her
        if(user == null){
            user = new EaseUser(username);
            EaseCommonUtils.setUserInitialLetter(user);
            final UserBean.Account[] account = {null};
            MyApplication.getInstance().getCurrentActivity().runOnUiThread(new Runnable() {
                public void run() {
                    account[0] =  AccountLoader.getUser(username);
                }
            });
            if(account[0] !=null) {
                user = new EaseUser(account[0].getId());
                user.setNickname(account[0].getNickname());
                user.setAvatar(account[0].getAvatar());
                userDao.saveContact(user);
            }

        }
        return user;
    }
慕侠2389804
浏览 648回答 5
5回答

一只萌萌小番薯

新开一个线程使用join等待结束处理了!Thread th = new Thread(new Runnable(){....});th.join();

Cats萌萌

我觉得你贴出的代码没法表现问题所在,其次使用了runOnUiThread 还是报错NetworkOnMainThreadException. 本来这个错误的意思就是在UI线程访问网络报错的,你把相关逻辑放到runOnUiThread还是UI线程,一样也会报错。根本没解决问题

牧羊人nacy

Rxjava 完美解決該問題。

炎炎设计

你的getUserInfo()是否是在UI线程调用?
随时随地看视频慕课网APP

相关分类

Java
我要回答