Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package apitest;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.net.HttpURLConnection;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import org.json.simple.JSONArray;
  18. import org.json.simple.JSONObject;
  19. import org.json.simple.parser.JSONParser;
  20. import org.json.simple.parser.ParseException;
  21.  
  22. /**
  23. *
  24. * @author Dell PC
  25. */
  26. public class Apitest {
  27. static ArrayList<String> classname_list = new ArrayList<String>( );
  28. static ArrayList<String> classid_list = new ArrayList<String>( );
  29.  
  30. /**
  31. * @param args the command line arguments
  32. */
  33. public static void main(String[] args) throws MalformedURLException, IOException, ParseException {
  34. // TODO code application logic here
  35. //getAddressByGpsCoordinates();
  36. classname();
  37. for (int i=0;i<classname_list.size();i++){
  38. System.out.println("Main Classname And ID :"+classname_list.get(i)+" "+classid_list.get(i));
  39. }
  40. }
  41.  
  42. public static void classname()
  43. throws MalformedURLException, IOException, org.json.simple.parser.ParseException {
  44.  
  45. URL url = new URL("http://hang404.com/class_info.php?api_key=hangjhqdfqjhsfhdfhdgjdgd83r7tgq87rg2f8r7g2f2874gf2");
  46. //'https://maps.googleapis.com/maps/api/geocode/json?latlng=23.73929,90.375586&key=AIzaSyCXx3ubAosxwUQH4i4gMo6j89RaUbdISz0'
  47. HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  48. String formattedClassname = "";
  49. String formattedClassId = "";
  50.  
  51. try {
  52. InputStream in = url.openStream();
  53.  
  54. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  55. String result, line = reader.readLine();
  56. result = line;
  57. //System.out.println("hiii"+result);
  58. while ((line = reader.readLine()) != null) {
  59. result += line;
  60. }
  61. //System.out.println("hiii"+result);
  62.  
  63. JSONParser parser = new JSONParser();
  64. JSONObject rsp = (JSONObject) parser.parse(result);
  65.  
  66. if (rsp.containsKey("items")) {
  67. JSONArray matches = (JSONArray) rsp.get("items");
  68. for (int i=0;i< matches.size();i++){
  69. JSONObject data = (JSONObject) matches.get(i); //TODO: check if idx=0 exists
  70. formattedClassname = (String) data.get("es_classname");
  71. formattedClassId = (String) data.get("es_classesid");
  72. classname_list.add(formattedClassname);
  73. classid_list.add(formattedClassId);
  74.  
  75. }
  76.  
  77.  
  78. }
  79. else{
  80. System.out.println("none");
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. //JSONObject data = (JSONObject) rsp.get(0); //TODO: check if idx=0 exists
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. } finally {
  95. urlConnection.disconnect();
  96.  
  97. }
  98. }
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement