我有以下Android代码与Web套接字程序连接。 Web Socket作为Java应用程序运行。 socket和android代码都在Eclipse中。
问题是,Android程序根本没有连接此套接字。它按下按钮点击处理,如下面的代码所示。在这一行socket = new Socket("192.168.1.21", 8081);
之后它会抛出异常,不知道为什么。我试图从互联网上引用所有示例代码,但我在下面的代码中看不到任何奇怪的问题。
请有人帮我解决这个问题吗?
Android代码:
mHandler = new Handler();
clientthread = new ClientThread();
Button connectBtn = (Button) findViewById(R.id.button2);
connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Start contacting Socket
mHandler.post(mUpdate);
}
});
private Runnable mUpdate = new Runnable() {
public void run() {
try {
clientthread.run();
pw = new PrintWriter(socket.getOutputStream(), true);
EditText randTxtField = (EditText) findViewById(R.id.editText1);
pw.println(randTxtField.getText().toString());
pw.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
// Read randrom ID returned by Socket
BufferedReader socketReader;
try {
socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
msgStr = socketReader.readLine();
Log.d("msgStr: ", msgStr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
class ClientThread implements Runnable {
@Override
public void run()
{
try {
// Exception throw when executing this line itself...
socket = new Socket("192.168.1.21", 8081);
}
catch (UnknownHostException e1) {
e1.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
例外:
03-24 14:51:54.672: D/dalvikvm(21673): GC_FOR_ALLOC freed 295K, 4% free 9800K/10128K, paused 19ms, total 20ms
03-24 14:54:19.974: D/socketServerIP(21673): 192.168.1.21
03-24 14:54:19.984: W/System.err(21673): android.os.NetworkOnMainThreadException
03-24 14:54:19.984: W/System.err(21673): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
03-24 14:54:19.984: W/System.err(21673): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
03-24 14:54:19.984: W/System.err(21673): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
03-24 14:54:19.984: W/System.err(21673): at libcore.io.IoBridge.connect(IoBridge.java:112)
03-24 14:54:19.984: W/System.err(21673): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-24 14:54:19.984: W/System.err(21673): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
03-24 14:54:19.984: W/System.err(21673): at java.net.Socket.startupSocket(Socket.java:567)
03-24 14:54:19.984: W/System.err(21673): at java.net.Socket.<init>(Socket.java:226)
03-24 14:54:19.984: W/System.err(21673): at com.example.cobrowseandroid.RandomIDActivity$ClientThread.run(RandomIDActivity.java:262)
03-24 14:54:19.984: W/System.err(21673): at com.example.cobrowseandroid.RandomIDActivity$1.run(RandomIDActivity.java:152)
03-24 14:54:19.984: W/System.err(21673): at android.os.Handler.handleCallback(Handler.java:733)
03-24 14:54:19.984: W/System.err(21673): at android.os.Handler.dispatchMessage(Handler.java:95)
03-24 14:54:19.984: W/System.err(21673): at android.os.Looper.loop(Looper.java:136)
03-24 14:54:19.984: W/System.err(21673): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-24 14:54:19.984: W/System.err(21673): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 14:54:19.984: W/System.err(21673): at java.lang.reflect.Method.invoke(Method.java:515)
03-24 14:54:19.984: W/System.err(21673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-24 14:54:19.984: W/System.err(21673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-24 14:54:19.984: W/System.err(21673): at dalvik.system.NativeStart.main(Native Method)
03-24 14:54:19.984: D/AndroidRuntime(21673): Shutting down VM
03-24 14:54:19.984: W/dalvikvm(21673): threadid=1: thread exiting with uncaught exception (group=0x415efba8)
03-24 14:54:19.984: E/AndroidRuntime(21673): FATAL EXCEPTION: main
03-24 14:54:19.984: E/AndroidRuntime(21673): Process: com.example.cobrowseandroid, PID: 21673
03-24 14:54:19.984: E/AndroidRuntime(21673): java.lang.NullPointerException
03-24 14:54:19.984: E/AndroidRuntime(21673): at com.example.cobrowseandroid.RandomIDActivity$1.run(RandomIDActivity.java:179)
03-24 14:54:19.984: E/AndroidRuntime(21673): at android.os.Handler.handleCallback(Handler.java:733)
03-24 14:54:19.984: E/AndroidRuntime(21673): at android.os.Handler.dispatchMessage(Handler.java:95)
03-24 14:54:19.984: E/AndroidRuntime(21673): at android.os.Looper.loop(Looper.java:136)
03-24 14:54:19.984: E/AndroidRuntime(21673): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-24 14:54:19.984: E/AndroidRuntime(21673): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 14:54:19.984: E/AndroidRuntime(21673): at java.lang.reflect.Method.invoke(Method.java:515)
03-24 14:54:19.984: E/AndroidRuntime(21673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-24 14:54:19.984: E/AndroidRuntime(21673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-24 14:54:19.984: E/AndroidRuntime(21673): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你不应该在UI线程上调用网络资料。
你正在打电话mHandler.post(mUpdate)
在当前线程(UI线程)方法“run”中运行。用于例如代替:
Thread t = new Thread(mUpdate);
t.start();
以分离的线程运行网络代码......
此外,不要忘记在UI线程中调用UI相关代码(例如getActivity()。runOnUiThread())。