Handling json response in post method android

825 Views Asked by At

I am a new to android. I have a rest API post method. I have to provide email and password to the API. It will provide the response which will be in json format like the format below

{
error: "This email is already registered "
status: 404
}

the raw response will be in the form as below

{"error":"This email is already registered\n","status":404}

I sent the name value pairs successfully but I don't know how to handle this type of response. Can anyone help me out with this? here is my code

protected String doInBackground(String... params) {

            //HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("https://zigron.goabode.com/api/registration/new");
        PostMethod post = new PostMethod("https://zigron.goabode.com/api/registration/new");

        String resp=null;

        try 
        {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("email", email_s ) );
            nameValuePairs.add(new BasicNameValuePair("password", pwd_s ) );

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            System.out.println(nameValuePairs);

            resp=post.getResponseBodyAsString();
            return resp;

        }

last two statements are not correct. I am handling that as a string which shouldn't be done

2

There are 2 best solutions below

2
AudioBubble On

You can use the below code for accessing Post Webservice...

public static List<NameValuePair> addMediaParameter() {
    List<NameValuePair> params = new LinkedList<NameValuePair>();
    params.add(new BasicNameValuePair("offset", "0"));
    params.add(new BasicNameValuePair("length", "100"));
    // params.add(new BasicNameValuePair("limit", "3"));
    return params;
}

public static String responseRetriever(String url,
        List<NameValuePair> values) {
    try {
        String paramString = "";
        if (values != null)
            paramString = URLEncodedUtils.format(values, "utf-8");

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url + paramString);
        HttpResponse response;
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            String data = EntityUtils.toString(entity);

            if (data != null && data.length() > 0)                  
                return data;
            else
                return null;
        }
    } catch (Exception e) {
        Log.e("responseRetriever", e.toString());
    }

    return null;
}



String response_data = responseRetriever("yourURL", addMediaParameter());

This response_data will give you the whole response of your webservice as String. Now you can parse the String as per your response : Like

 JSONObject jsonresponse = new JSONObject(response_data);
String error_message = jsonresponse.getString("error");

Parse as per your response.

0
Srujana Dakoju On

I am using https link for that I need to write a TrustManager. Android won't accept https by default where as http will.