作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道为什么 HttpURLConnection 在 android 上失败但在 java Eclipse 上成功。我已经面对这个问题很多天了,并试图解决它,但从未通过。我的例子的代码如下:
try {
url = "https://mobitrade.vpbs.com.vn:8080/getlistckindex/hnx";
URL urlGetSymbol = new URL(url);
HttpURLConnection con = (HttpURLConnection) urlGetSymbol.openConnection();
con.setRequestMethod("GET");
con.setReadTimeout(15000);
con.setConnectTimeout(15000);
con.setDoOutput(true);
con.connect();
int responseCode = con.getResponseCode();
BufferedReader in =new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String responseString = response.toString().replaceAll("\\[", "").replaceAll("\\]", "")
.replaceAll("\"", "");
} catch (IOException ioe) {
ioe.printStackTrace();
}
该代码在 java eclipse 中工作正常,但由于 con.connect() 出错而无法在 android 上工作。
最佳答案
我认为您正在为 HttpURLConnection 使用不同的 api 包,因此请检查一下。您可以使用 java.net.HttpURLConnection api 进行相同的操作。
关于java - 为什么 HttpURLConnection 在 android 上失败但在 java Eclipse 上成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395747/
我是一名优秀的程序员,十分优秀!