我試圖執行http post請求以在鏈接中指定:Click here for the link. 我該如何使用Java?如何使用java發送JSON數組字符串的HTTP發佈請求?
String url = "http://sentiment.vivekn.com/api/text/";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "Text to classify";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
我該如何修改這個鏈接中描述的發送JSON數組文本並檢索結果?
謝謝,它工作完美。 – Rahul
謝謝,這個工程。有些東西我不明白,但是:如果我將'int res'中的代碼省略到'disconnect'之前,它不再有效。此外,如果我省去了打印語句,服務器將消除由此產生的死代碼。爲什麼這些陳述是必要的? – Warkst