开发者社区 问答 正文

android 使用了runOnUiThread 还是报错NetworkOnMa?400报错

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;
    }

 

展开
收起
爱吃鱼的程序员 2020-06-07 22:12:20 610 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    `runOnUiThread`就是让代码在主线程上跑的,如果 

    AccountLoader.getUser()

    是请求网络部分的话,最简单的就是:

    newThread(newRunnable(){publicvoidrun(){finalUseruser=AccountLoader.getUser(username);MyApplication.getInstance().getCurrentActivity().runOnUiThread(newRunnable(){account[0]=user;});}}).start();

     

    2020-06-07 22:12:41
    赞同 展开评论
问答分类:
问答标签:
问答地址: