我想創建一個Http請求並將結果存儲在一個JSONObject中。我沒有用servlet工作太多,所以我不確定我是否1)正確地創建請求,2)應該創建JSONObject。我已經導入了JSONObject和JSONArray類,但是我不知道應該在哪裏使用它們。以下是我的:如何從Java Servlet中檢索JSON中的提要?
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
//create URL
try {
// With a single string.
URL url = new URL(FEED_URL);
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
}
catch (IOException e) {
}
我的FEED_URL已經寫好,它會返回格式爲JSON的提要。
這已經讓我幾個小時了。非常感謝,你們是非常寶貴的資源!