Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 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 Reply

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...