我有一个简单的程序,它发送一个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 + " ");
感谢Manu Rivas的评论,我发现了这个问题。我没有正确地将空格附加到我的php文件中的输出参数。我做了:
$spaces . ' ';
代替:
$spaces = $spaces . ' ';