UMA*_*MAR 10 android http-post
我正在使用以下代码进行httpPost调用,但是当我尝试在chrome扩展中的"简单休息客户端"中提供以下参数时,它返回了400个错误的请求它可以正常工作任何人指导我在这里做了什么错误?
简单休息客户端我输入以下内容:
URL:http://jon2012.com/api/register
方法:POST标题:没有标题,因为它们不是必需的数据:{"email":"test@example.com","first_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");
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会得到满足
UMA*_*MAR 21
我犯了一个常见错误,json对象的序列错了.例如,我发送它像first_name,email..etc..where正确的序列是电子邮件,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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54182 次 |
| 最近记录: |