how to make httpPost call with json encoded body?

后端 未结 2 673
栀梦
栀梦 2020-12-03 15:31

i am using following code to make an httpPost call but it is returning me 400 bad request when i try to give following parameters in \"simple rest client\" in chrome extensi

2条回答
  •  隐瞒了意图╮
    2020-12-03 15:55

    i was making a common mistake sequence of json object was wrong. for example i was sending it like first_name,email..etc..where as correct sequence was email,first_name

    my code

    boolean result = false;
            HttpClient hc = new DefaultHttpClient();
            String message;
    
            HttpPost p = new HttpPost(url);
            JSONObject object = new JSONObject();
            try {
    
                object.put("updates", updates);
                object.put("mobile", mobile);
                object.put("last_name", lastname);
                object.put("first_name", firstname);
                object.put("email", email);
    
            } catch (Exception ex) {
    
            }
    
            try {
            message = object.toString();
    
    
            p.setEntity(new StringEntity(message, "UTF8"));
            p.setHeader("Content-type", "application/json");
                HttpResponse resp = hc.execute(p);
                if (resp != null) {
                    if (resp.getStatusLine().getStatusCode() == 204)
                        result = true;
                }
    
                Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
            } catch (Exception e) {
                e.printStackTrace();
    
            }
    
            return result;
    

提交回复
热议问题