我得到java.net.SocketTimeoutException:当我使用以下代码发送GET请求时,传输端点未连接异常。此代码适用于其他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();
}
}
答案 0 :(得分:5)
实际上错过了许可
<uses-permission android:name="android.permission.INTERNET" />