创建简单的客户端套接字时无法理解异常

时间:2019-06-07 18:15:35

标签: android networkonmainthread

我正在尝试在android studio中创建一个简单的客户端套接字,但无法执行并且无法理解异常。每当我尝试创建连接时,我的应用程序就会崩溃。我的应用上只有一个名为“按钮”的按钮

public class MainActivity extends AppCompatActivity {

Socket s;
Button con;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    con = (Button)findViewById(R.id.button);
}
public void connect(View view)
{
    try {


        s = new Socket("192.168.12.178", 22222);

        if (s.isConnected())
        {
            Toast.makeText(getApplicationContext(), "Socket is Connected", Toast.LENGTH_SHORT).show();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

---------崩溃开始

06-06 23:11:49.143 22703-22703/com.example.test1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.test1, PID: 22703
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:5198) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
        at libcore.io.IoBridge.connect(IoBridge.java:122)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
        at java.net.Socket.startupSocket(Socket.java:592)
        at java.net.Socket.tryAllAddresses(Socket.java:128)
        at java.net.Socket.<init>(Socket.java:178)
        at java.net.Socket.<init>(Socket.java:150)
        at com.example.test1.MainActivity.connect(MainActivity.java:29)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:5198) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:0)

例外是:Caused by: android.os.NetworkOnMainThreadException 如果尝试在MainThread上执行网络操作,则会引发此异常。您必须在后台线程上执行此操作,您可以在此处查看操作方法:https://developer.android.com/reference/android/os/AsyncTask

答案 1 :(得分:0)

我很确定您的主要问题是:“由android.os.NetworkOnMainThreadException引起”,它在MainActivity.java第29行中抛出,并且因为在主UI线程上发出网络请求而被抛出您不能这样做,因为它会挂断UI线程。尝试使用AsyncTask。