66import java .io .InputStreamReader ;
77import java .lang .reflect .Array ;
88import java .net .HttpURLConnection ;
9+ import java .net .MalformedURLException ;
10+ import java .net .ProtocolException ;
911import java .net .URL ;
1012
1113import com .google .gson .Gson ;
@@ -28,53 +30,48 @@ public SimpleAPICache(String url, int limit, long cacheLifespan) {
2830 gson = new Gson ();
2931 }
3032
31- public MeeResponse getJson () {
33+ public MeeResponse getJson () throws IOException {
3234 if (cache != null && System .currentTimeMillis () - lastQuery < cacheLifespan ) {
3335 return cache ;
3436 }
3537
36- try {
37- int page = 0 ;
38- boolean contFlag = false ;
39-
40- do {
41- HttpURLConnection httpcon = (HttpURLConnection ) ((new URL (toBeCheck + "?limit=" + limit + "&page=" + page ).openConnection ()));
42- httpcon .setDoOutput (true );
43- httpcon .addRequestProperty ("User-Agent" , "Mozilla/5.0 (Windows NT 6.1; Win64; x64)" );
44- httpcon .setRequestMethod ("GET" );
45- httpcon .connect ();
46-
47- //Read response
48- InputStream is = httpcon .getInputStream ();
49- BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
50- String line = null ;
51- StringBuffer sb = new StringBuffer ();
52- while ((line = reader .readLine ()) != null ) {
53- sb .append (line );
54- }
55- is .close ();
56- httpcon .disconnect ();
57- //Get specified data needed.
58-
59- MeeResponse responseInstance = gson .fromJson (sb .toString (), MeeResponse .class );
60-
61- if (cache == null ) {
62- cache = gson .fromJson (sb .toString (), MeeResponse .class );
63- } else if (cache .getPlayers ().length != 0 ) {
64- cache .setPlayers (concatenate (cache .getPlayers (), responseInstance .getPlayers ()));
65- }
66-
67- if (responseInstance .getPlayers ().length == limit ) {
68- contFlag = true ;
69- page ++;
70- }
71- } while (contFlag );
38+ int page = 0 ;
39+ boolean contFlag = false ;
40+
41+ do {
42+ HttpURLConnection httpcon = (HttpURLConnection ) ((new URL (toBeCheck + "?limit=" + limit + "&page=" + page ).openConnection ()));
43+ httpcon .setDoOutput (true );
44+ httpcon .addRequestProperty ("User-Agent" , "Mozilla/5.0 (Windows NT 6.1; Win64; x64)" );
45+ httpcon .setRequestMethod ("GET" );
46+ httpcon .connect ();
7247
73- return cache ;
74- } catch (IOException e ) {
75- e .printStackTrace ();
76- return null ;
77- }
48+ //Read response
49+ InputStream is = httpcon .getInputStream ();
50+ BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
51+ String line = null ;
52+ StringBuffer sb = new StringBuffer ();
53+ while ((line = reader .readLine ()) != null ) {
54+ sb .append (line );
55+ }
56+ is .close ();
57+ httpcon .disconnect ();
58+ //Get specified data needed.
59+
60+ MeeResponse responseInstance = gson .fromJson (sb .toString (), MeeResponse .class );
61+
62+ if (cache == null ) {
63+ cache = gson .fromJson (sb .toString (), MeeResponse .class );
64+ } else if (cache .getPlayers ().length != 0 ) {
65+ cache .setPlayers (concatenate (cache .getPlayers (), responseInstance .getPlayers ()));
66+ }
67+
68+ if (responseInstance .getPlayers ().length == limit ) {
69+ contFlag = true ;
70+ page ++;
71+ }
72+ } while (contFlag );
73+
74+ return cache ;
7875 }
7976
8077 private static Players [] concatenate (Players [] a , Players [] b ) {
0 commit comments