Skip to content

Instantly share code, notes, and snippets.

@Keda87
Created December 25, 2013 05:03
Show Gist options
  • Save Keda87/8120319 to your computer and use it in GitHub Desktop.
Save Keda87/8120319 to your computer and use it in GitHub Desktop.
GET data from internet / api
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class JSONParser {
public static String getPlainJSON(String api) throws MalformedURLException, IOException {
URL url = new URL(api);
URLConnection urlc = url.openConnection();
StringBuilder builder;
try (InputStreamReader inStream = new InputStreamReader(urlc.getInputStream()); BufferedReader buff = new BufferedReader(inStream)) {
builder = new StringBuilder();
while (true) {
String nextLine = buff.readLine();
String NL = System.getProperty("line.separator");
if (nextLine != null) {
builder.append(nextLine).append(NL);
} else {
break;
}
}
}
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment