22 Votes

Android Programming: Receive Responce from HTTP POST Request

Tutorial by Progger99 | 2014-04-17 at 13:52

Some time ago, I have written a tutorial about how to send data via HTTP request to a server using an Android app. Bake at that time, I have not mentioned how to process the response of the server. Today, I want to do so.

In my tutorial, I have mentioned, I ended with the following line of code:

HttpResponse response = client.execute(post);

Here, "post" keeps our data and "client" is our HTTP client which sends the POST-data to the server by calling "execute()". For a detailed explanation of this, please refer to my old tutorial, which I have linked at the top.

In order to catch and process the data from the server, we store the result of "client.execute(post)" in an HttpResponse update. Here, we name this object with "response".

Read String from HttpResponse Object

Of course, the content of our HttpResponse object can be everything the server can return. In the simplest case, this is just a string containing of one line. To read out this string, we can proceed as follows:

HttpResponse response = client.execute(post);

HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
InputStreamReader inputstream = new InputStreamReader(content, "UTF-8");
BufferedReader reader = new BufferedReader(inputstream);

String s = reader.readLine();

In the first line, we are executing our POST request. After that, we build a "reader" to read the contents of "response". With this, it is important to determine the right encoding. You have to specify the encoding, the server is using for the output of the data. In most cases, this is "UTF-8". The determination of the encoding is optional, you can also omit it. At the end of this script, we store the result in the string "s".

Read multiline String

If our server is returning a string that is consisting of more than one line, we can expand our code in this way:

HttpResponse response = client.execute(post);
InputStreamReader inputstream = 
    new InputStreamReader(response.getEntity().getContent(), "UTF-8");
BufferedReader reader = new BufferedReader(inputstream);

StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
    builder.append(line).append("n");
}

String s = builder.toString();

The example is just the same as the code above, the only thing is, that we are now reading out multiple lines. To do so, we are using a StringBuilder and we are going through the result line by line.

Further Processing

After we have saved our data into a string, the question is, how to process the data further.

Next to simply display the string, further processing depends on the format in which our string is created. For example, we can think of the JSON format. In this case, we can parse our string using a JSON object.

Retrieve HTTP Status Code from Server

The HttpResponse object is not already containing the pure string that is probably returned by the server. In fact, you can use this object to get even more information about the request and the response. For example, the HTTP Status Code:

HttpResponse response = client.execute(post);

StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();

if (statusCode == 200) {
  HttpEntity entity = response.getEntity();
  InputStream content = entity.getContent();

  InputStreamReader inputstream = new InputStreamReader(content,"UTF-8");
  BufferedReader reader = new BufferedReader(inputstream);

  String s = reader.readLine();
}

Using response.getStatusLine().getStatusCode(), it is possible to get the status code of our POST request. In the example, we are checking, whether the code is 200 (OK) and we only go on with the reading if so.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Android Splash Screen Tutorial

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.