我有以下代碼:@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 關注
- 105 瀏覽
添加回答
舉報
0/150
提交
取消