Om *_*kla 0 android http http-put
我正在使用HttpPut与Android中的服务器通信,我得到的响应代码是500.在与服务器人交谈后,他说准备下面的字符串并发送.
{ "键": "值", "关键": "值"}
现在我完全感到困惑,我应该在我的请求中添加此字符串.
请帮帮我.
我最近不得不想办法让我的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();
}
Run Code Online (Sandbox Code Playgroud)
有了这个说法有一个更容易的选择,通过使用一个库,将为您生成更新方法,允许您更新记录,而无需像我上面那样手动编写代码.看起来很常见的2个库是odata4j和restlet.虽然我还没有找到一个明确的简单教程,因为odata4j有一个restlet非常好:http: //weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android -使用- restlet.aspx?CommentPosted =真#commentmessage
| 归档时间: |
|
| 查看次数: |
10842 次 |
| 最近记录: |