Android - java.net.SocketTimeoutException:传输端点未连接
当我使用以下代码发送 GET 请求时,出现 java.net.SocketTimeoutException: Transport Endpoint is notconnected 异常。不过,此代码适用于其他 GET 请求,但不适用于某一特定 URL。知道我可能做错了什么吗?
try {
URL mUrl = new URL(url);
urlConn = (HttpURLConnection) mUrl.openConnection();
urlConn.setReadTimeout(5000);
urlConn.setConnectTimeout(5000);
urlConn.setRequestMethod(requestMethod);
if (contentType != null)
urlConn.addRequestProperty("Content-Type", "application/"
+ contentType);
urlConn.setDoOutput(true);
if (query != null) {
urlConn.setRequestProperty("Content-Length",
Integer.toString(query.length()));
urlConn.getOutputStream().write(query.getBytes("UTF8"));
}
urlConn.connect();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
StringBuffer responseMsg = new StringBuffer();
InputStream dis = urlConn.getInputStream();
int chr;
while ((chr = dis.read()) != -1) {
responseMsg.append((char) chr);
}
return new Response(urlConn.getResponseCode(),
urlConn.getResponseMessage(),
responseMsg.toString());
}
return new Response(urlConn.getResponseCode(),
urlConn.getResponseMessage(), null);
} catch (IOException e) {
throw e;
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
I get the java.net.SocketTimeoutException: Transport endpoint is not connected exception when I use the following piece of code to send a GET request. This code works for other GET requests though, just not for one particular URL. Any idea what I might be doing wrong?
try {
URL mUrl = new URL(url);
urlConn = (HttpURLConnection) mUrl.openConnection();
urlConn.setReadTimeout(5000);
urlConn.setConnectTimeout(5000);
urlConn.setRequestMethod(requestMethod);
if (contentType != null)
urlConn.addRequestProperty("Content-Type", "application/"
+ contentType);
urlConn.setDoOutput(true);
if (query != null) {
urlConn.setRequestProperty("Content-Length",
Integer.toString(query.length()));
urlConn.getOutputStream().write(query.getBytes("UTF8"));
}
urlConn.connect();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
StringBuffer responseMsg = new StringBuffer();
InputStream dis = urlConn.getInputStream();
int chr;
while ((chr = dis.read()) != -1) {
responseMsg.append((char) chr);
}
return new Response(urlConn.getResponseCode(),
urlConn.getResponseMessage(),
responseMsg.toString());
}
return new Response(urlConn.getResponseCode(),
urlConn.getResponseMessage(), null);
} catch (IOException e) {
throw e;
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上是缺少权限
It was actually missing the permission