Advertisement
riking

I think this should work

Aug 16th, 2013
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1.     /**
  2.      * Returns a map of Mojang-defined URLs (such as session.minecraft.net) to
  3.      * a boolean of whether they are currently working or not.
  4.      * <p>
  5.      * If an error occurs, null is returned.
  6.      *
  7.      * @return map as specified above
  8.      */
  9.     public Map<String, Boolean> getMcStatus() {
  10.         URL checkUrl;
  11.         try {
  12.             checkUrl = new URL("http://status.mojang.com/check");
  13.         } catch (MalformedURLException impossible) {
  14.             return null;
  15.         }
  16.         InputStreamReader read = null;
  17.         HashMap<String, Boolean> ret = new HashMap<String, Boolean>();
  18.         try {
  19.             Object content = new JSONParser().parse(read = new InputStreamReader(checkUrl.openStream()));
  20.             read.close();
  21.             if (!(content instanceof JSONArray)) {
  22.                 // Bad / old format
  23.                 return null;
  24.             }
  25.             JSONArray arr = (JSONArray) content;
  26.             for (Object obj : arr) {
  27.                 if (obj instanceof JSONObject) {
  28.                     JSONObject jobj = (JSONObject) obj;
  29.                     Map.Entry<?, ?> entry = (Map.Entry<?, ?>) (jobj.entrySet()).iterator().next();
  30.                     Object key = entry.getKey();
  31.                     Object value = entry.getValue();
  32.                     String site = (String) key;
  33.                     boolean val = ((String) value).equalsIgnoreCase("green");
  34.                     ret.put(site, val);
  35.                 }
  36.             }
  37.             return ret;
  38.         } catch (ClassCastException e) {
  39.             e.printStackTrace();
  40.         } catch (IOException e) {
  41.             e.printStackTrace();
  42.         } catch (ParseException e) {
  43.             e.printStackTrace();
  44.         } finally {
  45.             tryClose(read);
  46.         }
  47.         return null;
  48.     }
  49.  
  50.     private void tryClose(Closeable c) {
  51.         if (c == null)
  52.             return;
  53.         try {
  54.             c.close();
  55.         } catch (IOException e) {
  56.             e.printStackTrace();
  57.         }
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement