0

I am trying to print the contents of the below URL on the console but it does not print anything: LINK

Please find below the code I am using:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;



public class CrunchifyCallUrlAndGetResponse {

    public static void main(String[] args) {
        System.out.println("\nOutput: \n" + callURL("http://en.wikipedia.org/w/api.php?format=json&action=query&titles=SMALL&prop=revisions&rvprop=content"));
    }

    public static String callURL(String myURL) {
        System.out.println("Requeted URL:" + myURL);
        StringBuilder sb = new StringBuilder();
        URLConnection urlConn = null;
        InputStreamReader in = null;
        try {
            URL url = new URL(myURL);
            urlConn = url.openConnection();
            if (urlConn != null)
                urlConn.setReadTimeout(60 * 1000);
            if (urlConn != null && urlConn.getInputStream() != null) {
                in = new InputStreamReader(urlConn.getInputStream(),
                        Charset.defaultCharset());
                BufferedReader bufferedReader = new BufferedReader(in);
                if (bufferedReader != null) {
                    int cp;
                    while ((cp = bufferedReader.read()) != -1) {
                        sb.append((char) cp);
                    }
                    bufferedReader.close();
                }
            }
        in.close();
        } catch (Exception e) {
            throw new RuntimeException("Exception while calling URL:"+ myURL, e);
        } 

        return sb.toString();
    }
}

Could you please help here as to how can I get data from the URl as I am not getting any error message as well but at the same time there is nothing that is getting printed even though the url has data.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user2077648
  • 853
  • 6
  • 22
  • 39

0 Answers0