Java examples for JSON:JSON Data
get Json Content from URL
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class Main { public static String getJsonContent(String path) { try {/*from w ww. j ava2 s .c o m*/ URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setConnectTimeout(3000); connection.setRequestMethod("GET"); connection.setDoInput(true); int code = connection.getResponseCode(); if (code == 200) { return changeInputStream(connection.getInputStream()); } } catch (Exception e) { e.printStackTrace(); } return ""; } public static String changeInputStream(InputStream inputStream) { String jsonString = ""; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int len = 0; byte[] data = new byte[1024]; try { while ((len = inputStream.read(data)) != -1) { byteArrayOutputStream.write(data, 0, len); } jsonString = new String(byteArrayOutputStream.toByteArray()); } catch (IOException e) { e.printStackTrace(); } return null; } }