Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

android - how to make httpPost call with json encoded body?

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 extension it works fine any one guide me what mistake am i doing here?

Simple rest Client I entered the following:

URL: http://jon2012.com/api/register Method: POST Headers: No headers, as they are not required Data: { "email": "[email protected]", "first_name":"Name" }enter image description here

Android Code:

HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        HttpResponse response;
        JSONObject json = new JSONObject();
        try{
            HttpPost post = new HttpPost(url);
            json.put("email", email);
            json.put("first_name", name);
            StringEntity se = new StringEntity( "JSON: " + json.toString());  
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(se);
            response = client.execute(post);
            /*Checking response */
            /*if(response!=null){
                InputStream in = response.getEntity().getContent(); //Get the data in the entity
*/
            int statusCode = response.getStatusLine().getStatusCode();

        }
        catch(Exception e){
            e.printStackTrace();
           // createDialog("Error", "Cannot Estabilish Connection");
        }

any help would be appriciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 the 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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...