gpt4 book ai didi

android - 使用 AsyncTask 时同步线程的问题,Android

转载 作者:行者123 更新时间:2023-11-29 22:27:23 25 4
gpt4 key购买 nike

我正在制作应用程序,使用套接字将数据从 Android 设备发送到服务器,然后反向发送。我正在使用 AsyncTask 类来处理连接。我的问题是,在我的主要 Activity 类中的某个时刻,我必须从输入流中读取,例如。

    conhandler = new ConnectionHandler();
conhandler.execute();
Log.i("AppOne", "Reading from stream");
message = conhandler.readFromStream();

发生的事情是,在我的 ConnectionHandler 类建立套接字连接之前执行此代码 fragment 。有什么方法可以解决这个问题?这是我的 ConnectionHandler 类的代码:

public class ConnectionHandler extends AsyncTask<Void, Void, Void>{

public static String serverip = "192.168.1.100";
public static int serverport = 7777;
Socket s;
public DataInputStream dis = null;
public DataOutputStream dos = null;
public int message;



@Override
protected Void doInBackground(Void... params) {

try {
Log.i("AsyncTank", "doInBackgoung: Creating Socket");
s = new Socket("192.168.1.100", 7777);
} catch (Exception e) {
Log.i("AsyncTank", "doInBackgoung: Cannot create Socket");
}
Log.i("AsyncTank", "doInBackgoung: Socket created");
if (s.isConnected()) {
try {
Log.i("AsyncTank", "doInBackgoung: inside input try");
dis = new DataInputStream(s.getInputStream());
Log.i("AsyncTank", "doInBackgoung: InputStream assigned");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
e.printStackTrace();
}
try {
Log.i("AsyncTank", "doInBackgoung: inside output try");
dos = new DataOutputStream(s.getOutputStream());
Log.i("AsyncTank", "doInBackgoung: OutputStream assigned");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
e.printStackTrace();
}
} else {
Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket is closed");
}

return null;
}

public void writeToStream(double lat, double lon) {
try {
Log.i("AsynkTask", "writeToStream : Writing lat");
dos.writeDouble(lat);
dos.flush();
} catch (Exception e) {
Log.i("AsynkTask", "writeToStream : Writing failed");
}
try {
Log.i("AsynkTask", "writeToStream : Writing lon");
dos.writeDouble(lon);
dos.flush();
} catch (Exception e) {
Log.i("AsynkTask", "writeToStream : Writing failed");
}
}

public int readFromStream() {
try {
if (s.isConnected()) {
Log.i("AsynkTask", "readFromStream : Reading message");
message = dis.readInt();
} else {
Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
}
} catch (Exception e) {
Log.i("AsynkTask", "readFromStream : Reading failed");
}
return message;
}

最佳答案

是的,您需要在ASyncTask 线程中执行所有 网络操作。您还可以在任务完成时执行操作,方法是重写 onPostExecute() 方法。那里的东西在 UI 线程上运行,因此您可以操纵 TextView 、布局等。

如何使用异步任务打开套接字并向其写入 double 值的示例:

public class ASyncExample extends Activity {

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
// set stuff up
super.onCreate(savedInstanceState);
}

private class WriteToServer extends AsyncTask<Double, Void, Void> {

private final String serverip = "192.168.1.100";
private final int serverport = 7777;
Socket s;
private DataOutputStream dos = null;

@Override
protected Void doInBackground(Double... params) {

try {

// open the stream
s = new Socket("192.168.1.100", 7777);
dos = new DataOutputStream(s.getOutputStream());

// write the passed double to the stream
dos.writeDouble(params[0]);
dos.flush();

} catch (Exception e) {
Log.i("AsyncTank", "something's gone wrong");
}

return null;
}
}
}

关于android - 使用 AsyncTask 时同步线程的问题,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5460872/

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