integrii
3/13/2015 - 9:09 PM

Simple web request with Java on Android

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();

            }