HttpURLConnection.getOutputStream() 需要 20 秒.为什么? [英] HttpURLConnection.getOutputStream() takes 20 seconds. Why?

查看:45
本文介绍了HttpURLConnection.getOutputStream() 需要 20 秒.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码片段在 android (2.3.4) 中执行网络服务调用.我的问题发生在 conn.getOutputStream 上,因为此方法似乎需要整整 20 秒才能返回.有什么想法吗?

I'm trying to perform webservice calls in android (2.3.4) using the following code snippet. My problem occurs on conn.getOutputStream, as this method seems to be taking a full 20 seconds to return. Any thoughts?

URL uri = new URL(serviceURI + "/remoteMethod");
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

Log.d(TAG, "opening output stream");
OutputStreamWriter writer = new OutputStreamWriter(
    conn.getOutputStream());
Log.d(TAG, "output stream opened");

String body = getRequestBody();
writer.write(body);
writer.flush();
writer.close();
int responseCode= conn.getResponseCode();

推荐答案

看来这可能是网络问题,而 jarnbjo 在评论中的建议实际上是正确的,即连接时间太长.我将服务器和设备移到另一个网络(Web 服务不是公开的),问题就解决了.

It would appear that this was probably a network problem, and that jarnbjo was actually correct in his suggestion in the comments that the connection was just taking too long. I moved the server and device to another network (webservice is not public), and the problem cleared up.

这篇关于HttpURLConnection.getOutputStream() 需要 20 秒.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆