我有以下代码:@Controllerpublic class GatesController { @RequestMapping ("/gates") public static String qualityGates(String x) throws IOException { try { System.out.println("\n------QualityGates------"); URL toConnect = new URL(x); HttpURLConnection con = (HttpURLConnection) toConnect.openConnection(); System.out.println("Sending 'GET' request to URL : " + x); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //Cast the JSON-File to a JSONObject JSONObject res = new JSONObject(response.toString()); JSONArray gates = new JSONArray(res.getJSONObject("projectStatus").getJSONArray("conditions").toString()); JSONObject test = new JSONObject(res.getJSONObject("projectStatus").toString()); String a = ("\nThe current Project-Status is: " + test.get("status") + "\n"); String b = ""; for (int i = 0; i < gates.length(); i++) { String status = gates.getJSONObject(i).getString("status"); String metric = gates.getJSONObject(i).getString("metricKey"); b = b + ("<\b>Status: " + status + " | Metric: " + metric); } System.out.println(a+b); return a + b; } catch (Exception e) { System.out.println(e); return String.format("Error"); } }问题是目前该网站如下所示:网站_当前但我希望每个指标都换行,而不是在一行中。正如您所看到的,我尝试在字符串内涵处添加 <\b> 您知道如何解决这个问题吗?这是我的第一个网络应用程序,我有点卡住了。我感谢每一个帮助!
1 回答
紫衣仙女
TA贡献1839条经验 获得超15个赞
你的“<\b>”打破了它。如果删除它并添加换行符“\n”,它应该可以工作。像这样:
String a = ("\nThe current Project-Status is: " + test.get("status") + "\n");
String b = "";
for (int i = 0; i < gates.length(); i++) {
status = gates.getJSONObject(i).getString("status");
String metric = gates.getJSONObject(i).getString("metricKey");
b = b + ("Status: " + status + " | Metric: " + metric + "\n");
}
您还返回纯文本。因此,要正确显示它,请添加“products =”text/plain”以返回格式化的字符串。
@GetMapping(value = "/gates", produces = "text/plain")
然后你的输出将显示换行符。这样您就可以应用进一步的格式。
- 1 回答
- 0 关注
- 66 浏览
添加回答
举报
0/150
提交
取消