Java HTTP Get Json getJSON(HttpURLConnection conn)

Here you can find the source of getJSON(HttpURLConnection conn)

Description

get JSON

License

Open Source License

Declaration

public static JSONObject getJSON(HttpURLConnection conn) 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.io.OutputStream;

import java.net.HttpURLConnection;

import org.json.JSONObject;

public class Main {
    public static JSONObject getJSON(HttpURLConnection conn) throws IOException {
        JSONObject job = null;//  w w w .j  av a2s.  com
        conn.setRequestMethod("GET");
        // write it out yo
        OutputStream os = conn.getOutputStream();
        conn.connect();
        //   OutputStreamWriter out = new OutputStreamWriter(os);
        //   out.flush();

        // out.write(jsonObject.toString());
        //   out.close();

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

        StringBuffer sb = new StringBuffer();
        String s = "";
        while ((s = in.readLine()) != null) {
            sb.append(s);
        }
        System.out.println("\nREST Service Invoked Successfully..");
        in.close();

        // os.write(job.toString().getBytes("UTF-8"));
        os.close();
        return job;
    }
}

Related

  1. getJSON(String url)
  2. getJSON(URL url)