简体   繁体   English

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

[英]How to GET a string from a URL without omitting spaces in JAVA

I have a simple program that sends a GET request and fetches a string. 我有一个发送GET请求并获取字符串的简单程序。 The URL is a PHP page I wrote that fetches some data from a database on the server and parses it into a string with a bunch of white-space characters (which I need). URL是我写的一个PHP页面,它从服务器上的数据库中获取一些数据,并将其解析为带有一串空格字符的字符串(我需要)。 The problem is the spaces get omitted in the response to the java app, and my question is how to avoid omitting them? 问题是在对Java应用程序的响应中空格被忽略了,我的问题是如何避免忽略它们?

My java code: 我的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());

original string: 原始字串:

200329951 123 3 03/09/18

the string that gets returned from the request: 从请求返回的字符串:

200329951123303/09/18

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

Thanks to Manu Rivas's comment, i found the problem. 感谢Manu Rivas的评论,我找到了问题所在。 I wasn't appending the spaces to the output parameter in my php file correctly. 我没有在我的php文件中正确地将空格附加到输出参数。 I did: 我做了:

$spaces . ' ';

instead of: 代替:

$spaces = $spaces . ' ';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM