gpt4 book ai didi

java - JAVA中如何从URL中获取不省略空格的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:28 24 4
gpt4 key购买 nike

我有一个简单的程序,可以发送 GET 请求并获取字符串。该 URL 是我编写的一个 PHP 页面,它从服务器上的数据库中获取一些数据,并将其解析为带有一堆空格字符(我需要)的字符串。问题是在对 java 应用程序的响应中省略了空格,我的问题是如何避免省略它们?

我的java代码:

URL servicesUrlObj = new URL("https://myurl.mysite.com/placeholder.php");
HttpURLConnection connection = (HttpURLConnection) servicesUrlObj.openConnection();

connection.setRequestMethod("GET");

int responseCode = connection.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + servicesUrlObj);
System.out.println("Response Code : " + responseCode);

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();

while ((inputLine = reader.readLine()) != null) {
content.append(inputLine);
}
reader.close();

System.out.println(content.toString());

原始字符串:

200329951 123 3 03/09/18

从请求返回的字符串:

200329951123303/09/18

最佳答案

这不是一个非常优雅的解决方案,但您可以考虑在 while 循环中添加 content.append("") ,甚至只是将当前行转换为 content.append(inputLine + "");

关于java - JAVA中如何从URL中获取不省略空格的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52149937/

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