我正在使用HttpUrlConnection的应用程序,与服务器连接的漂亮,但从其中获取数据时说连接超时IOException。Android httpUrlConnection连接到服务器,但不是互联网
互联网,&网络权限已在android.manifest中设置;有没有酒吧出现在Android模拟器(这是否说什么)。
阅读在developer.android.com: 模拟器的功能限制包括: - 用于确定网络连接状态 不支持 - 和其他几个....
任何帮助将得到高度赞赏。而且我没有真正的设备来测试它。
接收到的服务器信息确实打印在logcat中。
谢谢...
下面是代码:
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
Log.i(INFO_TAG, "Received server:" + conn.toString());
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-length", "0");
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setConnectTimeout(25000 /* milliseconds */);
conn.setReadTimeout(15000/* 10000 *//* milliseconds */);
// conn.setDoInput(true);
// Starts the query
conn.connect();
status = conn.getResponseCode();
Log.d(DEBUG_TAG, "Status recevied is: " + conn.getResponseCode());
responseCode = status;
if (responseCode == 200) {
Log.i(INFO_TAG, "URL Connection OK");
contentIs = conn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(contentIs));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
Log.i(INFO_TAG, "Data Read is: " + line);
}
} else {
Log.d(DEBUG_TAG, "Could not read data from web server");
Log.e(ConnectAndGetData.class.toString(),
"Failed to download content");
}
}// end of try
catch (Exception ex) {
// This is currently being printout
Log.d(DEBUG_TAG, "Received an exception" + ex.toString(), ex);
ex.printStackTrace();
throw new IOException("Error Connecting" + ex.toString());// "Error connecting");
} finally {
if (contentIs != null) {
contentIs.close();
conn.disconnect();
}
}
一两件事,因为,它抛出一个连接超时异常不会在调试该行信息被打印: Log.d (DEBUG_TAG,“收到的状态是:”+ conn.getResponseCode());
上传带问题的代码。 – Lucifer
嗨路西法,已经上传代码的问题:)我希望有人可以暗示的东西在这里... – AliR
我想我得到了答案,尝试'conn.close();'而不是'如果'conn.disconnect(); “这两种方法都有不同的含义。 – Lucifer