我看到了一个在线示例,说明如何在 pc 上的 android 应用程序和 java 服务器之间实现套接字连接。该应用程序在模拟器(运行 2.3.3)上完美运行,但在我的安卓智能手机上强制关闭。
经过一些研究,我得出结论,解决方案是实现 asynctask 方法。但是我在知道如何实施它时遇到了问题。我看到了很多解释,但它们对我的情况没有帮助。这是我正在使用的示例代码,我将将此代码用作我开发的任何其他应用程序的参考模板。
我知道这个问题已经被问了很多(很多很多!),但直到现在我还没有设法完成这项工作。那么任何人都可以帮助我使用以下代码:
package com.exercise.AndroidClient;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidClient extends Activity {
EditText textOut;
TextView textIn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOut = (EditText)findViewById(R.id.textout);
Button buttonSend = (Button)findViewById(R.id.send);
textIn = (TextView)findViewById(R.id.textin);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("192.168.1.101", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
;
}};
}
日志猫
03-03 21:35:59.643: E/Trace(3472): error opening trace file: No such file or directory (2)
03-03 21:36:01.073: D/gralloc_goldfish(3472): Emulator without GPU emulation detected.
03-03 21:36:12.045: D/AndroidRuntime(3472): Shutting down VM
03-03 21:36:12.045: W/dalvikvm(3472): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-03 21:36:12.103: E/AndroidRuntime(3472): FATAL EXCEPTION: main
03-03 21:36:12.103: E/AndroidRuntime(3472): android.os.NetworkOnMainThreadException
03-03 21:36:12.103: E/AndroidRuntime(3472): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
03-03 21:36:12.103: E/AndroidRuntime(3472): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
03-03 21:36:12.103: E/AndroidRuntime(3472): at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
03-03 21:36:12.103: E/AndroidRuntime(3472): at libcore.io.Streams.readFully(Streams.java:81)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.io.DataInputStream.readShort(DataInputStream.java:169)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:182)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.io.DataInputStream.readUTF(DataInputStream.java:186)
03-03 21:36:12.103: E/AndroidRuntime(3472): at com.exercise.AndroidClient.AndroidClient$LongOperation.onPostExecute(AndroidClient.java:80)
03-03 21:36:12.103: E/AndroidRuntime(3472): at com.exercise.AndroidClient.AndroidClient$LongOperation.onPostExecute(AndroidClient.java:1)
03-03 21:36:12.103: E/AndroidRuntime(3472): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-03 21:36:12.103: E/AndroidRuntime(3472): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-03 21:36:12.103: E/AndroidRuntime(3472): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-03 21:36:12.103: E/AndroidRuntime(3472): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 21:36:12.103: E/AndroidRuntime(3472): at android.os.Looper.loop(Looper.java:137)
03-03 21:36:12.103: E/AndroidRuntime(3472): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 21:36:12.103: E/AndroidRuntime(3472): at java.lang.reflect.Method.invoke(Method.java:511)
03-03 21:36:12.103: E/AndroidRuntime(3472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 21:36:12.103: E/AndroidRuntime(3472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 21:36:12.103: E/AndroidRuntime(3472): at dalvik.system.NativeStart.main(Native Method)
03-03 21:36:14.722: I/Process(3472): Sending signal. PID: 3472 SIG: 9