gpt4 book ai didi

java - HttpURLConnection 没有收到响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:22:46 28 4
gpt4 key购买 nike

我在线程的循环中调用了 WebService 代码。我使用不同的参数构建 URL,并使用 HttpURLConnection 调用 WS,如下所示:

public void run() {
//establish connection to db in another class

while(true) {
//Get args value from another class

for(String arg : args) {
try {
String invokeWS = "URL is built here with encoded args";

URL obj = new URL(invokeWS);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
con.disconnect();

System.out.println(response.toString());
} catch (UnsupportedEncodingException e) {
System.out.println("Unsupported Encoding Exception");
} catch (MalformedURLException e) {
System.out.println("Malformed URL Exception");
} catch (IOException e) {
System.out.println("IO Exception");
}
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Interrupted Exception");
}
}
}

但是,如果网络连接在发送“GET”之后和收到响应之前断开,则控制永远不会返回到循环中的线程。我在这里做错了什么?

最佳答案

避免在 runnable 中循环。我遇到了同样的问题,我减少了我的循环。我直接加载了数据,然后在我的末端循环了可运行的任务。

关于java - HttpURLConnection 没有收到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51319008/

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