Complementary Post : Making HTTP Request in Android

30 sec read

URL url = new URL(urlToHit);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

 

if we look above code snippet we will find java.net.URL class is imported. HttpURLConnection Class from the same package is used. Also we are making GET request.

 

connection.setDoInput(true);

mConnectionCode = connection.getResponseCode();

if (mConnectionCode == HttpURLConnection.HTTP_OK) {

    InputStream inputStream = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));

    String line = "";

    while ((line = rd.readLine()) != null) {
        responseString.append(line);
    }

    return true;
}

 

if we are successful in making our GET request and getting response from server , connection.getInputStream() will return an  Input stream which can be read line by line and its data can be stored in a String buffer.

since String buffer contains response from server this String buffer can be utilised by our application.

 

Thanks