Last active
August 29, 2015 14:04
-
-
Save MiguelCatalan/e36a610f4a3a8d08008a to your computer and use it in GitHub Desktop.
Class in charge of the connections management with AutomaticAPIREST
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
/** | |
* Class in charge of the connections management with AutomaticAPIREST | |
* Created by Miguel Catalan Bañuls on 29/07/14. @mcatalanb | |
*/ | |
public class AARManager { | |
public static final String USER_AGENT = "Mozilla/5.0"; | |
private String URL; | |
private String user_agent; | |
/** | |
* Default constructor | |
* | |
* @param url location of the resource | |
*/ | |
public AARManager(String url) { | |
this.URL = url; | |
this.user_agent = USER_AGENT; | |
} | |
/** | |
* Optional constructor, can set a different User Agent | |
* | |
* @param url location of the resource | |
* @param user_agent user agent to be used by the HttpURLConnection | |
*/ | |
public AARManager(String url, String user_agent) { | |
this.URL = url; | |
this.user_agent = user_agent; | |
} | |
/** | |
* Gets the raw data from the resource | |
* | |
* @return raw json string | |
*/ | |
public String getRawResource() throws JSONException, IOException, NetworkErrorException { | |
return downloadData(URL, user_agent); | |
} | |
/** | |
* Gets the useful stuff from the data tag | |
* | |
* @return String with the raw data ready to use | |
* @throws IOException | |
* @throws JSONException | |
*/ | |
public String getJsonData() throws JSONException, IOException, NetworkErrorException { | |
String response = downloadData(URL, user_agent); | |
JSONObject jsonObject = new JSONObject(response); | |
String strData = jsonObject.getString("data"); //we only get the data from the label data, so clever. | |
if (strData != null) { | |
return strData; | |
} else { | |
throw new NullPointerException("There is no info"); | |
} | |
} | |
/** | |
* Connects to the url given and gets the raw data | |
* | |
* @param url location of the resource | |
* @param user user agent to be used by the HttpURLConnection | |
* @return raw json string | |
* @throws IOException | |
* @throws JSONException | |
* @throws NetworkErrorException | |
*/ | |
private String downloadData(String url, String user) throws IOException, JSONException, NetworkErrorException { | |
if (BaseApplication.isOnline()) { | |
HttpURLConnection con; | |
BufferedReader in; | |
StringBuffer response; | |
URL obj = new URL(url); | |
con = (HttpURLConnection) obj.openConnection(); | |
con.setRequestMethod("GET"); | |
con.setRequestProperty("User-Agent", user); | |
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { | |
in = new BufferedReader( | |
new InputStreamReader(con.getInputStream())); | |
String inputLine; | |
response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
return response.toString(); | |
} else { | |
throw new HttpResponseException(HttpURLConnection.HTTP_UNAVAILABLE, "Can't get the response"); | |
} | |
} else { | |
throw new NetworkErrorException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment