gpt4 book ai didi

android 异步任务 NetworkOnMainThreadException

转载 作者:行者123 更新时间:2023-11-29 14:17:50 27 4
gpt4 key购买 nike

我正在尝试使用套接字在 android 中编写一个服务器/客户端应用程序,并且我在 AsyncTask 中处理客户端套接字(服务器不是 android,只是普通的 java)。当我得到异常时我正在尝试从服务器读取。我发现当我从 android list 中删除 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;
}
}

这是我的主要 Activity

  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);
}
}

最佳答案

这个问题有两个解决方案。

1) 不要在 Main UIThread 中编写网络调用,为此使用 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;

关于android 异步任务 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13604397/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com