Android套接字连接麻烦

时间:2016-12-23 19:52:59

标签: java android sockets client-server

我制作了一个客户端(android(java)) - server(node.js)应用程序。

问题是,当我使用AVD模拟器(使用10.0.2.2或扩展IP)来测试我的程序时,ewerything运行良好,但是当我尝试使用手机或其他模拟器(genimotion,nox,bluestacks)时也是如此我根本无法连接。

用户必须使用互联网和Wi-Fi。连接功能的代码是:

    public void Connect(String Host, int Port)
{
    try
    {
        System.out.println(Handlers.isEmpty());
        if (Handlers.isEmpty())
            throw new NullPointerException ("No handlers");

        if (Socket != null && Socket.isConnected())
            Socket.close();

        InetAddress IpAddress = InetAddress.getByName(Host);
        Address = new InetSocketAddress(IpAddress, Port);
        Socket = new Socket();
        Socket.setSoTimeout(1000);
        Socket.connect(Address, 1500);
        InputStream = new BufferedReader (new InputStreamReader(Socket.getInputStream()));
        OutputStream = new PrintWriter (Socket.getOutputStream());
        RThread = new ReadSocketThread(InputStream, Handlers, SearchName);
        ReadSocket = new Thread(RThread);
        ReadSocket.start();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

}

并且控制台说:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: bobko.alexandr.consultant, PID: 1599
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:4438)
                  at android.view.View$PerformClick.run(View.java:18422)
                  at android.os.Handler.handleCallback(Handler.java:733)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5045)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:4438) 
                  at android.view.View$PerformClick.run(View.java:18422) 
                  at android.os.Handler.handleCallback(Handler.java:733) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:136) 
                  at android.app.ActivityThread.main(ActivityThread.java:5045) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                  at dalvik.system.NativeStart.main(Native Method) 
               Caused by: android.os.NetworkOnMainThreadException
                  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
                  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
                  at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
                  at libcore.io.IoBridge.connect(IoBridge.java:112)
                  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
                  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
                  at java.net.Socket.connect(Socket.java:843)
                  at bobko.alexandr.consultant.STAPLibrary.STAPLibraryMain.Connect(STAPLibraryMain.java:53)
                  at bobko.alexandr.consultant.MainActivity.Create(MainActivity.java:29)
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:4438) 
                  at android.view.View$PerformClick.run(View.java:18422) 
                  at android.os.Handler.handleCallback(Handler.java:733) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:136) 
                  at android.app.ActivityThread.main(ActivityThread.java:5045) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                  at dalvik.system.NativeStart.main(Native Method) 

申请已终止。

1 个答案:

答案 0 :(得分:0)

当你在主线程NetworkOnMainThreadException中执行网络操作时,会抛出。这就是你无法连接的原因。

您应该在AsyncTask内包装连接到网络的代码,或者使用Retrofit之类的库。简单地说,网络操作必须在主UI线程以外的线程中运行。