我在android开发方面相当新颖。这里是我的问题:安卓系统中的HTTP POST问题
我有这个端点:http://bdzservice.apphb.com/api/Image
它接受POST请求 请求的主体是一个字符串,例如:
/SearchServlet?action=showMap&id1=25&date=09/12/2013&st1=5216000&st2=5229030
无效的例子:{"/SearchServlet?action=showMap&id1=25&date=09/12/2013&st1=5216000&st2=5229030"}
无效例2:{mapHref : "/SearchServlet?action=showMap&id1=25&date=09/12/2013&st1=5216000&st2=5229030"}
无效示例3:{"mapHref" : "/SearchServlet?action=showMap&id1=25&date=09/12/2013&st1=5216000&st2=5229030"}
这是我迄今为止编写的代码:
HttpClient client = new DefaultHttpClient();
String message;
HttpPost httpPost = new HttpPost("http://bdzservice.apphb.com/api/Image");
try
{
message = url;
StringEntity se = new StringEntity(message, "UTF8");
se.setContentType("application/json");
httpPost.setEntity(se);
httpPost.setHeader("Content-type", "application/json");
HttpResponse resp = client.execute(httpPost);
if (resp != null)
{
if (resp.getStatusLine().getStatusCode() == 204)
result = true;
}
Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
}
我试图发布数据时总是出错,但是当我手动(通过REST客户端)发布数据时,我得到了一个OK结果。
有人可以帮助我吗?
编辑
This is the endpoint,这是写在C#(网页API)
编辑2:试图修改的服务来回报体它收到(见链接的评论),它retrns null,所以问题是没有得到身体(或只是读错了)
内部服务器错误 –
也许问题是内容类型?你是从你的REST客户端设置的吗?您发送的数据似乎不是JSON格式。 – SiN
内部服务器错误是不够的,请尝试读取错误日志。 – Matteo