Skip to content

Instantly share code, notes, and snippets.

Created March 18, 2017 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/823f2cec7013a67b03e8cad7a31b904d to your computer and use it in GitHub Desktop.
Save anonymous/823f2cec7013a67b03e8cad7a31b904d to your computer and use it in GitHub Desktop.
VK json example
import java.io.InputStreamReader;
import java.net.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class vkJson {
static JSONParser jsonParser = new JSONParser();
public static void main(final String[] args) throws Exception {
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress("192.168.0.2", 3128)
);
// URL url = new URL("http://school.ioffe.ru/");
URL url = new URL("https://api.vk.com/method/users.get?" +
"lang=en&user_ids=7373");
HttpURLConnection connection = (HttpURLConnection)
url.openConnection(proxy);
// BufferedReader br = new BufferedReader(
// new InputStreamReader(
// connection.getInputStream()/*, "KOI8-R"*/));
// String s;
// while ((s = br.readLine()) != null) {
// System.out.println(s);
// }
// br.close();
Object response = jsonParser.parse(new InputStreamReader(
connection.getInputStream()));
if (response instanceof Long) {
System.out.println(((Long) response) * 17);
}
JSONObject responseObject = (JSONObject) response;
if (responseObject.containsKey("error")) {
System.out.println(((JSONObject) responseObject.get("error")).get("error_msg"));
return;
}
JSONArray array = (JSONArray) responseObject.get("response");
if (array.isEmpty()) {
System.out.println("Invalid user id");
return;
}
System.out.println(((JSONObject) array.get(0)).get("last_name"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment