Android 到计算机的 tcp 连接

Android tcp connection to computer

我是 android 的新手。 我试图构建一个应用程序,将字符串从 android 移动设备发送到我电脑上的 java 客户端。 这是服务器端:

public class Ser {

public static void main(String[] args) throws IOException {
    ServerSocket ss = new ServerSocket(12345);
    Socket s = ss.accept();
    InputStream is = s.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    System.out.println("message received from server is :");
    String ms = br.readLine();
    System.out.println("" + ms);
}

}

这是客户端:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Socket s= null;
    try {
    s = new Socket("192.168.0.102",12345);
    OutputStream os=s.getOutputStream();
    PrintWriter pw=new PrintWriter(os);
    pw.println("Hey!!! I am from client side ");
    pw.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

但这给出了以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ankitanand.udp/com.example.ankitanand.udp.MainActivity}: android.os.NetworkOnMainThreadException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
        at android.app.ActivityThread.access0(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4895)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
        at libcore.io.IoBridge.connect(IoBridge.java:112)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.Socket.startupSocket(Socket.java:566)
        at java.net.Socket.tryAllAddresses(Socket.java:127)
        at java.net.Socket.<init>(Socket.java:177)
        at java.net.Socket.<init>(Socket.java:149)
        at com.example.ankitanand.udp.MainActivity.onCreate(MainActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:5163)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)

我电脑的ip是192.168.0.102 我也在清单文件中使用了以下权限

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

您不能在主线程上执行网络操作。尝试使用 AsyncTask

另请查看有关 Network on Main Thread Exeption 的页面。

这样你的代码就可以工作了。