问题描述
我用下面的code作出httpPost呼叫,但它返回我的400错误的请求当我试图给在Chrome扩展按照简单的REST客户端参数,它工作正常,任何一个指导我什么错误,我在这里干什么?
i am using following code to make an httpPost call but it is returning me 400 bad requestwhen 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?
简单的REST客户端我进了以下内容:
Simple rest Client I entered the following:
网址: http://jon2012.com/api/register 方法:POST头:否标头,因为它们不要求数据:{电子邮件:[email protected],FIRST_NAME:姓名}
URL: http://jon2012.com/api/registerMethod: POSTHeaders: No headers, as they are not requiredData: { "email": "[email protected]", "first_name":"Name" }
Android的code:
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");
}
任何帮助将appriciated
any help would be appriciated
推荐答案
我正在JSON对象的一个常见的错误顺序是错误的。例如,我会发送它像FIRST_NAME,email..etc..where为正确顺序为电子邮件,FIRST_NAME
i was making a common mistake sequence of json object was wrong.for example i was sending it likefirst_name,email..etc..where as correct sequence was email,first_name
我的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;
这篇关于如何让httpPost使用JSON EN codeD身体打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!