gpt4 book ai didi

java - Android HttpURLConnection 错误

转载 作者:行者123 更新时间:2023-11-29 05:10:38 25 4
gpt4 key购买 nike

我的 Android 项目有问题,

我想用 get 方法调用 php 页面,我使用 HttpURLConnection,当我运行此应用程序时出现错误“应用程序已停止”

这是我的代码

    public void someFunction() throws IOException {

String inputLine = "";

URL url = new URL("http://www.domain.com/demo.php?test=abc");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}

} finally{
in.close();
}
}

我该怎么办?

谢谢。

最佳答案

我已经用下面的代码解决了这个问题,

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

关于java - Android HttpURLConnection 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28686520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com