gpt4 book ai didi

java - 我没有收到网页的回复

转载 作者:行者123 更新时间:2023-11-29 09:06:57 25 4
gpt4 key购买 nike

我是 Java/Android 的新手,我有一段代码可以获取网页的响应。我有这样的代码,但由于某些原因,BufferedReaderin.readLine() 总是返回 null。

URL url = new URL(endPoint);
connection = (HttpURLConnection) url.openConnection();

connection.setConnectTimeout(connectionTimeout);
connection.setReadTimeout(readTimeout);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(encodedParams.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

// Setup the POST payload
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(encodedParams);
wr.flush();
wr.close();

// Read the response
InputStream inputStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

不确定我在哪里犯了错误。

提前致谢。

最佳答案

尝试

connection.connect();

阅读回复之前

关于java - 我没有收到网页的回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14457999/

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