我正在使用HttpPut与安卓系统的服务器进行通信,得到的响应码是500。在与服务器人员交谈后,他说准备如下字符串并发送。
{"key":"value","key":"value"}
现在我完全迷惑了,我不知道应该在我的请求中把这个字符串添加到哪里。
请帮帮我。
发布于 2012-06-19 23:02:52
最近,我不得不想办法让我的android应用程序与WCF服务进行通信,并更新特定的记录。起初,这真的让我很难弄明白,主要是因为我对HTTP
协议了解不够多,但我能够通过使用以下命令来创建PUT
:
URL url = new URL("http://(...your service...).svc/(...your table name...)(...ID of record trying to update...)");
//--This code works for updating a record from the feed--
HttpPut httpPut = new HttpPut(url.toString());
JSONStringer json = new JSONStringer()
.object()
.key("your tables column name...").value("...updated value...")
.endObject();
StringEntity entity = new StringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
httpPut.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPut);
HttpEntity entity1 = response.getEntity();
if(entity1 != null&&(response.getStatusLine().getStatusCode()==201||response.getStatusLine().getStatusCode()==200))
{
//--just so that you can view the response, this is optional--
int sc = response.getStatusLine().getStatusCode();
String sl = response.getStatusLine().getReasonPhrase();
}
else
{
int sc = response.getStatusLine().getStatusCode();
String sl = response.getStatusLine().getReasonPhrase();
}
如上所述,有一个更简单的选择,那就是使用一个库,它将为您生成更新方法,允许您更新记录,而不必像我上面那样手动编写代码。看起来很常见的两个库是odata4j
和restlet
。虽然我还没能找到一个简单明了的odata4j
教程,但有一个非常好的restlet
教程:http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx?CommentPosted=true#commentmessage
https://stackoverflow.com/questions/10432885
复制相似问题