您好,我对 TCP 很陌生,我想构建一个多人游戏。但是我遇到了一个致命错误,每次打开“Client.java”类时我的应用程序都会崩溃。
我得到一个 android.os.NetworkOnMainThreadException,我已经在 android 清单中添加了 INTERNET 权限。我从另一个使用 Intent 去这个类,是的,我有 setOnClickListener(this); 在那里并将 Activity 添加到manufest,因此它不会因此而崩溃。如果这是一个好主意,我在应用程序中构建 TCP 服务器和客户端,那么我将如何使其成为公共主机?就像我需要为主机付费然后在服务器和客户端中使用 ip/port 一样?
无论如何,这是代码:
package com.theory.game;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Client extends Activity {
/** Called when the activity is first created. */
Scanner scanner = new Scanner(System.in);
//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
//StrictMode.setThreadPolicy(policy);
final EditText msg = (EditText) findViewById(R.id.etMsg);
Button send = (Button) findViewById(R.id.bSend);
final TextView convo = (TextView) findViewById(R.id.tvConvo);
final TextView status = (TextView) findViewById(R.id.tvStatus);
try {
send.setOnClickListener(new View.OnClickListener() {
Socket s = new Socket("10.0.2.2", 8080);
String message = msg.getText().toString();
@Override
public void onClick(View v) {
status.setText("...");
PrintWriter outp = null;
BufferedReader inp = null;
status.setText("Established connection..");
String serverMsg = null;
try {
outp = new PrintWriter(s.getOutputStream(), true);
inp = new BufferedReader(new InputStreamReader(s.getInputStream()));
serverMsg = inp.readLine();
} catch (IOException e) {
e.printStackTrace();
}
convo.append(serverMsg + "\n");
if (message != null) {
if (msg.getText().toString().trim() == "QUIT") {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
status.setText("Disconnected from server.");
} else {
try {
convo.append(message + "\n");
outp.println(message);
serverMsg = inp.readLine();
convo.append(serverMsg + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
status.setText("Problem in connection..!");
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
`FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
android.os.NetworkOnMainThreadException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(706): at android.app.ActivityThread.handleLaunchActivity
E/AndroidRuntime(706): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(706): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-13 22:53:18.542: E/AndroidRuntime(706): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(706): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(706): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(706): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(706): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(706): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(706): Caused by: android.os.NetworkOnMainThreadException
E/AndroidRuntime(706): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
E/AndroidRuntime(706): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
E/AndroidRuntime(706): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
E/AndroidRuntime(706): at libcore.io.IoBridge.connect(IoBridge.java:112)
E/AndroidRuntime(706): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
E/AndroidRuntime(706): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
E/AndroidRuntime(706): at java.net.Socket.startupSocket(Socket.java:566)
E/AndroidRuntime(706): at java.net.Socket.tryAllAddresses(Socket.java:127)
E/AndroidRuntime(706): at java.net.Socket.<init>(Socket.java:177)
E/AndroidRuntime(706): at java.net.Socket.<init>(Socket.java:149)
E/AndroidRuntime(706): at com.theory.game.Client$1.<init>(Client.java:35)
E/AndroidRuntime(706): at com.theory.game.Client.onCreate(Client.java:33)
E/AndroidRuntime(706): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(706): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(706): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime(706): ... 11 more `