【发布时间】:2020-07-19 02:53:01
【问题描述】:
由于某种原因,我的 android 应用程序没有从我的服务器接收任何数据。奇怪的是,我编写的一个不同的套接字客户端(它在我的计算机上运行,而不是在 AVD 上运行)接收并打印所有服务器发送的消息而没有错误。它使用与doInBackground 方法中的代码类似的代码。
public class Client extends AsyncTask<Void, Void, Void> {
int port;
Socket s;
@Override
protected Void doInBackground(Void... voids) {
try {
port = 1818;
s = new Socket("xx.xx.xx.xx", port);
if (!s.isConnected()) {
s.close();
}
BufferedReader re = new BufferedReader(new InputStreamReader(s.getInputStream()));
String temp = null;
while ((temp = re.readLine()) != null)
{
MainActivity.changeT(temp); // This will replace the TextView's text with temp.
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
我在想一个可能的解决方案是将 while 循环放到一个单独的线程中,但我不确定。欢迎任何建议! :-)
【问题讨论】: