Java HTTP Get getJsonFromUrl(String url)

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

Description

get Json From Url

License

Open Source License

Declaration

public static String getJsonFromUrl(String url) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String getJsonFromUrl(String url) throws IOException {
        URL urll = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) urll.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }//from   w  w  w .ja v a  2  s  .c o m

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        StringBuilder stringa = new StringBuilder();
        while ((output = br.readLine()) != null) {
            stringa.append(output);
        }

        conn.disconnect();
        return stringa.toString();
    }
}

Related

  1. doGet(String urlString)
  2. doGet(URL url, String... args)
  3. doGetRequest(String urlStr)
  4. executeGet(String targetURL)
  5. getHttpURLConnectionByGET(String resourceURL)
  6. httpGet(String httpUrl)
  7. httpGet(String url)
  8. httpGet(String url, boolean logStdout)
  9. httpGet(String url, StringBuffer response)