This question already has answers here:
How do I fix 'android.os.NetworkOnMainThreadException'?
                                
                                    (55个答案)
                                
                        
                                5年前关闭。
            
                    
它一直在xmppConnection.connect()上给我错误;

public static final String CHAT_SERVER = QBChatUtils.getChatServerDomain();
public void getusers()
{
    XMPPConnection xmppConnection = new XMPPConnection(CHAT_SERVER);
    try {
        // Chat action 1 -- create connection.
        Connection.DEBUG_ENABLED = true;
        xmppConnection.connect();
        xmppConnection.login(chatLogin, password);
        Roster roster = xmppConnection.getRoster();
        Collection<RosterEntry> entries = roster.getEntries();
        Presence presence;

        for(RosterEntry entry : entries) {
            presence = roster.getPresence(entry.getUser());

            System.out.println(entry.getUser());
            System.out.println(presence.getType().name());
            System.out.println(presence.getStatus());
        }
    } catch (XMPPException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


错误报告是这样的

11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2785)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.app.ActivityThread.access$1900(ActivityThread.java:150)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1395)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.os.Looper.loop(Looper.java:213)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.app.ActivityThread.main(ActivityThread.java:5225)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at java.lang.reflect.Method.invoke(Method.java:525)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at dalvik.system.NativeStart.main(Native Method)
11-06 15:24:08.904: E/AndroidRuntime(10981): Caused by: android.os.NetworkOnMainThreadException
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1142)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at org.jivesoftware.smack.proxy.DirectSocketFactory.createSocket(DirectSocketFactory.java:30)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:547)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:994)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at com.momena.chat.MyChatController.getusers(MyChatController.java:32)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at com.momena.chat.MessagesService.onStart(MessagesService.java:46)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.app.Service.onStartCommand(Service.java:450)
11-06 15:24:08.904: E/AndroidRuntime(10981):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2768)
11-06 15:24:08.904: E/AndroidRuntime(10981):    ... 10 more

最佳答案

您不能在主线程或ui线程中运行后台进程或长时间运行的进程,因此必须使用ServicesAsyncTask

10-07 15:43