Java URL Download get(String url)

Here you can find the source of get(String url)

Description

GET mechanism

License

Open Source License

Parameter

Parameter Description
url a parameter

Return

JSON generated by backend

Declaration

public static JSONObject get(String url) 

Method Source Code


//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;
    }
}

Related

  1. fetchUrl(URL url)
  2. fetchURLContents(String url)
  3. get(String url)
  4. get(String url)
  5. get(String url)
  6. get(String url, int timeout, Map header)
  7. get(String url, Map headers)
  8. get(String url, Map params)
  9. get(String url, String encoding)