Here you can find the source of get(String url)
Parameter | Description |
---|---|
url | a parameter |
public static JSONObject get(String url)
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import org.json.JSONException; import org.json.JSONObject; public class Main { private final static String CHARSET = "UTF-8"; /**/* ww w . j ava2 s. co m*/ * GET mechanism * @param url * @return JSON generated by backend */ public static JSONObject get(String url) { try { InputStream response = new URL(url).openStream(); BufferedReader streamReader = new BufferedReader(new InputStreamReader(response, CHARSET)); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = streamReader.readLine()) != null) { responseStrBuilder.append(inputStr); } try { JSONObject json = new JSONObject(responseStrBuilder.toString()); return json; } catch (JSONException e) { System.out.println("Failed reading JSON"); } } catch (MalformedURLException e) { System.out.println("Invalid URL"); } catch (IOException e) { System.out.println("Invalid IO"); } return null; } }