0
try {         

   url = new URL(https_url);
   HttpsURLConnection posts = (HttpsURLConnection)url.openConnection();

} catch (MalformedURLException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}  

How can I convert the HttpsURLConnection posts object to JSONObject? Thanks in advance. :)

Answer: using the mvn org.json

Community
  • 1
  • 1

3 Answers3

1
  1. Look for a JSON-library (there are dozens out there, from fancy stuff like GSON, Jackson, etc. to more lightweight ones).
  2. Read its documentation.
  3. Pass the InputStream directly, wrap it in a reader or read it as a String, depending on the API.
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Silverclaw
  • 1,316
  • 2
  • 15
  • 28
0

You can try GSon to work with Json, though I doubt you really need put to jSon posts object. The other case if you want to put HttpsURLConnection response to json. By the way you can create jSon object with plain string like: "

String json = "{"+"hello"+":"+"hello I am json Object"+"}";
P_M
  • 2,723
  • 4
  • 29
  • 62
0

I assume there is a mistake here. The object that you are creating is a connection object. There is no JSON to extract. If you want to get the JSON from the URL , this is sample code below.

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.oracle.com/");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}
Jeevan Varughese
  • 2,159
  • 2
  • 15
  • 20