Created
March 13, 2015 21:09
-
-
Save integrii/242063a7f4c1fed398cd to your computer and use it in GitHub Desktop.
Simple web request with Java on Android
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
URL apiUrl = null; | |
int statusCode = 0; | |
String responseData = ""; | |
try { | |
// setup new web request parameters | |
apiUrl = new URL("http://api.site.com"); | |
HttpURLConnection connection = null; | |
connection = (HttpURLConnection) apiUrl.openConnection(); | |
connection.setRequestProperty("Accept-Encoding", "identity"); // disables gzip compression | |
// make the request | |
connection.connect(); | |
statusCode = connection.getResponseCode(); | |
InputStream thisIsStupid = connection.getInputStream(); | |
Reader reader = new InputStreamReader(thisIsStupid); | |
int contentLength = connection.getContentLength(); | |
char charArray[] = new char[contentLength]; | |
reader.read(charArray); | |
responseData = new String(charArray); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please, stop learning to code in Java and stop teaching people to code in Java. We aren't going to ever make this terrible language die as long as people accept this level of terrible as 'OK'. This is not 'OK'.