我正在尝试使用套接字在android中编写服务器/客户端应用程序,我在AsyncTask
处理客户端套接字(服务器不是android,只是普通的java)。我在尝试时得到异常从服务器读取。我发现当我从android清单中删除android:targetSdkVersion="16"
时,异常消失了,我可以从服务器读取。
我不明白为什么会这样?任何人都可以帮我澄清一下吗?我也有理解asynctask方法doInBackground
和我自己的方法如何相关的问题。 conhandler.execute()
运行doInBackground()
然后等待我调用其他方法吗?谢谢你的帮助。
public class ConnectionHandler extends AsyncTask<Void, Void, Void>{
public static String serverip = "10.0.2.2";
public static int serverport = 5000;
Socket s;
PrintWriter out;
BufferedReader in;
protected Void doInBackground(Void... params) {
try {
s = new Socket(serverip, serverport);
Log.i("AsyncTank", "doInBackgoung: Created Socket");
}...
if (s.isConnected()) {
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(), true);
Log.i("AsyncTank", "doInBackgoung: Socket created, Streams assigned");
} ....
}
public void writeToStream(String message) {
try {
if (s.isConnected()){
out.println(message);
} else {
Log.i("AsynkTask", "writeToStream : Cannot write to stream, Socket is closed");
}
} catch (Exception e) {
Log.i("AsynkTask", "writeToStream : Writing failed");
}
}
public String readFromStream() {
try {
if (s.isConnected()) {
Log.i("AsynkTask", "readFromStream : Reading message");
String ret=in.readLine();
Log.i("AsynkTask", "readFromStream : read "+ret);
return ret;
} else {
Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
}
} catch (Exception e) {
Log.i("AsynkTask", "readFromStream : Reading failed"+e.getClass());
}
return null;
}
}
这是我的主要活动
public class MainActivity extends Activity {
private EditText view_email;
private EditText view_password;
TextView result;
ConnectionHandler conhandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conhandler = new ConnectionHandler();
conhandler.execute();
}
public void register(View view) {
view_email= (EditText) findViewById(R.id.email);
view_password = (EditText) findViewById(R.id.password);
String email=view_email.getText().toString();
String password=view_password.getText().toString();
conhandler.writeToStream("register");
conhandler.writeToStream(email);
conhandler.writeToStream(password);
String res=conhandler.readFromStream(); //here's the exception
result=(TextView) findViewById(R.id.result);
result.setText(res);
}
}
答案 0 :(得分:-1)
这个问题有两个解决方案。
1)不要在Main UIThread,Use Async Task中编写网络调用。
2)在setContentView(R.layout.activity_main)之后将下面的代码写入MainActivity文件;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
将import语句放到你的java文件中。
import android.os.StrictMode;
答案 1 :(得分:-2)
android.os.NetworkOnMainThreadException
这个恐怖来自 HoneyComb(3.0或更高版本)。
你不能在其主线程上执行网络操作,因为文档说。要获得这个,你必须使用handler或asynctask。 AFAIK没有其他方法可以做到。
您可以查看此详细信息WHY ICS Crashes your App
尝试使用以下代码段
new Thread(){
public void run(){
//do your Code Here
}
}.start();