【发布时间】:2019-10-04 08:25:38
【问题描述】:
每次有人点击我的 firebase 聊天室应用中的发送按钮时,我都会尝试发送 HTTP 请求。该请求与 Firebase 无关。但对于其中的某些功能很重要。
每次我点击发送按钮时,我的应用都会有点滞后。此外,每次我在发送此 http 请求之前先调用 Firebase 函数时,我的应用程序都会完全冻结...
只调用 Firebase 超级流畅...
我还想通过 http 连接将打字信号发送到不同的服务器。但由于这种滞后,这几乎是不可能的......
这是我的代码,
public static String post(URL url, String data) throws IOException {
StringBuilder out = new StringBuilder();
final CountDownLatch latch = new CountDownLatch(1);
Thread httpThread = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", userAgent);
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream());
writer.write(data);
writer.flush();
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
try {
while ((line = input.readLine()) != null) {
out.append(line).append("\n");
}
} finally {
input.close();
}
latch.countDown();
} catch (Exception e) {
}
}
});
httpThread.start();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Sending Output : "+out);
return out.toString().trim();
}
另外,我尝试使用特定的 AsyncTask 类进行测试,但我遇到了同样的延迟或冻结问题。我究竟做错了什么? :(
【问题讨论】:
标签: java android android-asynctask httpconnection