我正在使用以下代码进行httpPost调用,但是它返回400个不正确的请求
当我尝试在chrome扩展中的“简单休息客户端”中提供以下参数时,任何一个都可以正常工作指导我在这里做什么错误?
当我尝试在chrome扩展中的“简单休息客户端”中提供以下参数时,任何一个都可以正常工作指导我在这里做什么错误?
简单休息客户我输入以下内容:
网址:http://jon2012.com/api/register
方法:POST
标题:没有标题,因为它们不是必需的
数据:{“email”:“test@example.com”,“first_name”:“Name”}
Android代码:
- 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");
- }
任何帮助都会被贬低
解决方法
我正在犯错误的json对象的常见错误序列.
例如我正在发送它
first_name,email..etc …其中正确的序列是电子邮件,first_name
例如我正在发送它
first_name,email..etc …其中正确的序列是电子邮件,first_name
我的代码
- 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;