Java HTTP Get Json getJSON(URL url)

Here you can find the source of getJSON(URL url)

Description

get JSON

License

Apache License

Declaration

public static JSONObject getJSON(URL url) throws IOException, JSONException 

Method Source Code


//package com.java2s;
/*//from ww  w. j av a2  s.c  om
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
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 JSONObject getJSON(String url) throws IOException, JSONException {
        return getJSON(new URL(url));
    }

    public static JSONObject getJSON(URL url) throws IOException, JSONException {
        String body = get(url);
        JSONTokener tokener = new JSONTokener(body);

        return new JSONObject(tokener);
    }

    public static String get(String url) throws IOException {
        return get(new URL(url));
    }

    public static String get(URL url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();
        return response.toString();
    }
}

Related

  1. getJSON(HttpURLConnection conn)
  2. getJSON(String url)