0

I need to be able to get the http header code in android which is going to be generated by a php code.

I am very new to android and not able to grasp the correct way to do it.

Currently the code I have on my signup.java file is able to post some data to the php based webservice and depending on the data sent the webservice echo a json encoded response.

The php code for response when an error occurs is

$response["error"] = true;
$response["message"] = "Username Already taken";
echoRespnse(400,$response);

and the code on success is

$response["error"] = false;
$response["message"] = "Successfuly Registered";
echoRespnse(201,$response);

This will generate a json ecoded response message.

My signup.java file has the following code

public void post() throws UnsupportedEncodingException
    {
        // Get user defined values
        uname = username.getText().toString();
        email   = mail.getText().toString();
        password   = pass.getText().toString();
        confirmpass   = cpass.getText().toString();
        phone = phn.getText().toString();

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = null;
        HttpPost httppost = new HttpPost("http://www.rgbpallete.in/led/api/signup");
        if (password.equals(confirmpass)) {
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
                nameValuePairs.add(new BasicNameValuePair("uname", uname));
                nameValuePairs.add(new BasicNameValuePair("pass", password));
                nameValuePairs.add(new BasicNameValuePair("email", email));
                nameValuePairs.add(new BasicNameValuePair("phone", phone));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpResponse = httpclient.execute(httppost); //http header response??
                //Code to check if user was successfully created
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            Toast.makeText(getBaseContext(), "Password mismatch", Toast.LENGTH_SHORT).show();
            //Reset password fields
            pass.setText("");
            cpass.setText("");
        }

    }

I want to know if httpResponse will hold the http header response code being generated by the webservice or will I need to parse the json response message to understand what is being returned by the webserivce.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
Rick Roy
  • 1,656
  • 2
  • 21
  • 47
  • possible duplicate of [Android HttpResponse response Code](http://stackoverflow.com/questions/8148765/android-httpresponse-response-code) – njzk2 Dec 18 '14 at 21:07
  • Not really, I have 2 questions in here one if using http header is the correct way and 2 if `httpResponse = httpclient.execute(httppost)` will hold the http header code. Didn't want to know how to get the `http header` – Rick Roy Dec 18 '14 at 21:08
  • it is not clear what you are asking. what header are you trying to retrieve, and what for? The actual format of what you will receive depends on the implementation of `echoRespnse` in your php. by `http header code` I assume you mean the status code? – njzk2 Dec 18 '14 at 21:15
  • (As a side note, try `Strin message = new JSONObject(EntityUtils.toString(httpResponse.getEntity())).getString("message");` once you have tested that the status code is 201.) – njzk2 Dec 18 '14 at 21:17

1 Answers1

3

The response code can be retrieved this way:

final int statusCode = httpResponse.getStatusLine().getStatusCode();
switch (statusCode)
{
    case 201:
        // Successfuly Registered
        break;
    case 400: 
        // Username Already taken
        break;
    default:
        break;
}

To list the header values, you can use:

for (final Header h : httpResponse.getAllHeaders())
{
    Log.d("Responser Header", h.getName() + ": " + h.getValue()); // or h.getElements()
}

or pick the desired header directly:

final String[] contentLength = httpResponse.getHeaders("Content-Length");

and parse it.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • Will just using the first part of the code `statusCode` do or do I need to list the header values too? – Rick Roy Dec 18 '14 at 21:07
  • 1
    For identifying the status code, the first part is enough. If you need any header value (like specific messages), you may want to user the 2.1 or rather 2.2 part of the answer too. – rekaszeru Dec 18 '14 at 21:09