VK json example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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