Menu

HTTP Request on Android

One day after switching my android projects from API-19 to API-23 my lovely DefaultHttpClient class makes me troubles during compilation project. It just disappeared. :(

To find out a new way was easy. And now I want to share it. But before...

What is HTTP GET:

  • While making a HTTP GET request, data sent is basically included in the URL itself.
  • HTTP GET data can be stored in cache memory of browser/app.
  • Can not send while dealing with sensitive data like username and password etc (e.g: http://www.example.com/login?email=user@mail.com&password=pass).

In the above URL it can be observed that the sensitive data like username and password are directly send in URL which is basically not preferable while dealing with sensitive data.

What is HTTP POST:

  • While making HTTP POST request, data are not send directly but in some encrypted format.
  • HTTP POST requests are a bit more secure and can’t be cached.
  • Preferable while dealing with sensitive data like username and password.

In the above URL data are not included in URL, but in some encrypted format.

Note:

Don’t run these code directly in UI/Main thread. Create an AsyncTask and put the code in it.

Example 1 (POST):

URL url = new URL("http://myserver.com"); // URL for request

String sParameters = "a=1&b=2&c=3"; // POST data

HttpsURLConnection urlConnection = null;

BufferedReader input = null;

StringBuilder returnString = new StringBuilder();


try {

    urlConnection = (HttpsURLConnection) url.openConnection();

    urlConnection.setRequestMethod("POST");

    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    urlConnection.setRequestProperty("Content-Length","" + Integer.toString(sParameters.getBytes().length));

    urlConnection.setRequestProperty("Content-Language", "en-US");

    urlConnection.setRequestProperty("Accept", "application/json");

    urlConnection.setUseCaches(false);

    urlConnection.setDoInput(true);

    urlConnection.setDoOutput(true);


    // Send request

    DataOutputStream output = new DataOutputStream(urlConnection.getOutputStream());

    output.writeBytes(sParameters);

    output.flush();

    output.close();


    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"), 8);

    String line = null;

    while ((line = reader.readLine()) != null) {

        returnString.append(line);

    }

} catch(Exception e) {

    e.printStackTrace();

} finally {

    if(urlConnection != null) {

        urlConnection.disconnect();

    }

}

// in this point the returnString contain received data as string

Example 2 (GET):

URL url = new URL("http://myserver.com"); // URL for request

String sParameters = "a=1&b=2&c=3"; // POST data

HttpsURLConnection urlConnection = null;

BufferedReader input = null;

StringBuilder returnString = new StringBuilder();


try {

    urlConnection = (HttpsURLConnection) url.openConnection();

    urlConnection.setRequestMethod("GET");

    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    urlConnection.setRequestProperty("Content-Length",sParameters != null ? ("" + Integer.toString(sParameters.getBytes().length)) : "0");

    urlConnection.setRequestProperty("Content-Language", "en-US");

    urlConnection.setRequestProperty("Accept", "application/json");

    urlConnection.setUseCaches(false);

    urlConnection.setDoInput(true);

    urlConnection.setDoOutput(sParameters != null);


    // Send request

    if(sParameters != null) {

        DataOutputStream output = new DataOutputStream(urlConnection.getOutputStream());

        output.writeBytes(sParameters);

        output.flush();

        output.close();

    }


    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"), 8);

    String line = null;

    while ((line = reader.readLine()) != null) {

        returnString.append(line);

    }

} catch(Exception e) {

    e.printStackTrace();

} finally {

    if(urlConnection != null) {

        urlConnection.disconnect();

    }

}

// in this point the returnString contain received data as string

Two examples looks almast the same, except one line:

urlConnection.setRequestMethod("GET"); or urlConnection.setRequestMethod("POST");

Have a nice coding.

Tags:
android, java, http, request, get, post, example, api, api23, HttpsURLConnection

Keep your comment...