gpt4 book ai didi

java - 套接字线程在接收时阻塞主线程

转载 作者:搜寻专家 更新时间:2023-11-01 09:13:01 24 4
gpt4 key购买 nike

我正在使用套接字线程。发送请求消息后,大约需要 5 到 10 秒才能收到消息。在那段时间里,我希望我的主线程显示“请稍候”弹出窗口。该程序的流程看起来像这样。

  1. 显示弹出窗口
  2. 创建套接字线程。-> 这将连接到服务器
  3. 向服务器发送请求消息
  4. 接收消息。

我的问题是显示弹出窗口没有显示,直到套接字线程收到它的消息。

谁能告诉我这个问题的解决方法?

公共(public)类 LoginActivity 扩展 Activity {

.... <some coded>

public void onClickLogin(View view) {
Log.d(this.toString(), "onClickLogin");

showLoginLoadingPopup();

String login_id = ((EditText)findViewById(R.id.login_id)).getText().toString();
String login_pwd = ((EditText)findViewById(R.id.login_pwd)).getText().toString();

conn = new Connection(handler, 1, null);
conn.start();
conn.sendData(Connection.SSPH_USERCERT, new String[] {login_id, login_pwd});
}

公共(public)类 Connection extends Thread implements ConnectionConstant {

private InetAddress serverAddr;
private int serverPort;
private Socket socket;
PrintWriter out;
BufferedReader in;
private Handler handler;

public Connection(Handler h, int type, ServerClass server) {
Log.d(this.toString(), "Conncetion");
setServerInfo(type, server);
handler = h;

try {
connect();
} catch (Exception e) {
Log.e(this.toString(), "Error", e);
}
}

public void run() {
Log.d(this.toString(), "run");
try {
queue();
disconnect();
} catch (Exception e) {
Log.i(this.toString(), "Information", e);
}
}

private void connect() throws Exception {
if (serverAddr != null)
Log.d(this.toString(), "connect " + serverAddr.getHostName() + "("
+ Integer.toString(serverPort) + ")");
else
Log.d(this.toString(), "connect ");
socket = new Socket(serverAddr, serverPort);
socket.setSoLinger(true, 3000);
// UTF-8
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.i(this.toString(), "Socket connected!");
}

private void queue() throws Exception {
Log.d(this.toString(), "queue");
while (true) {
String sRcv = null;
sRcv = receive();

if (sRcv.length() > 0)
parseData(sRcv);

Thread.sleep(500);
Thread.yield();
}
}

private void send(String str) throws IOException {
Log.d(this.toString(), "send");
if (!socket.isConnected())
return;
Log.i(this.toString(), "Send : " + str);
out.println(str);
}

private String receive() throws Exception {
Log.d(this.toString(), "receive");
if (!socket.isConnected())
return null;

StringBuilder sb = new StringBuilder();
String str = "";

while ((str = in.readLine()) != null) {
Log.i(this.toString(), "Receive : " + str);
sb.append(str + "\n");
}
return sb.toString();
}

最佳答案

使用异步任务:

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
// show dialog
}

@Override
protected Void doInBackground(Void... params) {
// connect to the server
}

@Override
protected void onPostExecute(Void result) {
// close dialog
}
};

task.execute();

onPreExecute()onPostExecute()onProgressUpdate() 在 UI 线程上调用。

doInBackground() 在后台线程上被调用。

关于 AsyncTask 的更多信息:http://developer.android.com/reference/android/os/AsyncTask.html

关于java - 套接字线程在接收时阻塞主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6873901/

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