首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Http Put请求

Http Put请求
EN

Stack Overflow用户
提问于 2012-05-03 21:54:57
回答 1查看 11.6K关注 0票数 0

我正在使用HttpPut与安卓系统的服务器进行通信,得到的响应码是500。在与服务器人员交谈后,他说准备如下字符串并发送。

{"key":"value","key":"value"}

现在我完全迷惑了,我不知道应该在我的请求中把这个字符串添加到哪里。

请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2012-06-19 23:02:52

最近,我不得不想办法让我的android应用程序与WCF服务进行通信,并更新特定的记录。起初,这真的让我很难弄明白,主要是因为我对HTTP协议了解不够多,但我能够通过使用以下命令来创建PUT

代码语言:javascript
运行
复制
    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();
    }

如上所述,有一个更简单的选择,那就是使用一个库,它将为您生成更新方法,允许您更新记录,而不必像我上面那样手动编写代码。看起来很常见的两个库是odata4jrestlet。虽然我还没能找到一个简单明了的odata4j教程,但有一个非常好的restlet教程:http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx?CommentPosted=true#commentmessage

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10432885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档