Skip to content

Instantly share code, notes, and snippets.

@integrii
Created March 13, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save integrii/242063a7f4c1fed398cd to your computer and use it in GitHub Desktop.
Save integrii/242063a7f4c1fed398cd to your computer and use it in GitHub Desktop.
Simple web request with Java on Android
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();
}
@integrii
Copy link
Author

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'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment