Chlebta
Chlebta

Reputation: 3110

Sending JSON request POST in android

I'm trying to send json post request to : http://5.101.97.65:3000/en/api/sessions

Post Request Structure :

{user: {email: 'value', password: 'value '} }

Response should be like this :

{"success":true Or False,"info":"...","data":{"auth_token":"...."}}

For this example normaly it should return true value for "success" key :

{"user":{"email":"[email protected]","password":"changeme"}}

And this is my code :

final String myUrl ="http://5.101.97.65:3000/api/sessions";
        String result = null;
        String params = "{\"user\":{\"email\":\"[email protected]\",\"password\":\"changeme\"}}";
        try {
            restHandler rh = new restHandler();
            result = rh.getResponse(myUrl, 2, params);
            JSONObject rs = new JSONObject(result);
            if (rs.getBoolean("success"))
                return "good";
            else
                return "baad";
        }
        catch (Exception e){
            e.printStackTrace();
            return "baaad";
        }

The restHandler Class (function getResponse only ) :

public String getResponse (String url, int method, String params) {
        try {
            // http client
            HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == 2) {
                HttpPost httpPost = new HttpPost(url);
                if (params != null)
                {
                    httpPost.setHeader("Content-type", "application/json");
                    httpPost.setEntity(new StringEntity(params, "UTF8"));
                }
                httpResponse = httpClient.execute(httpPost);

            } else if (method == 1) {
                // code for httpGet not needed here
            }

            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
            httpClient = null;
            httpEntity = null;
            httpResponse = null;


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

The problem that I'm getting always as result the HTML code of error page not even the json response. So what's wrong in my code ?

Upvotes: 1

Views: 869

Answers (1)

Chlebta
Chlebta

Reputation: 3110

The problem was that when I send Post Request is always sended as PlainText so I have changed this :

HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());

To this :

HttpClient httpClient = new DefaultHttpClient();

And then I have added :

httpPost.addHeader("Accept", "application/json");

To slove the problem of 422 unprocessable entity on the response.

Final Code (The updated part only)

HttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            if (params != null)
            {
                httpPost.setHeader("Content-type", "application/json");
                httpPost.addHeader("Accept", "application/json");
                httpPost.setEntity(new StringEntity(params));

            }

Upvotes: 2

Related Questions