POSTing from Android to WCF REST web service: how to retrieve passed json data

时间:2021-02-04 13:21:15

I'm trying to contact a RESTful WCF POST web service from an android client which should pass some data in json format. I've already succesfully contacted a RESTful WCF GET web service, but I cannot figure out how the POST version works.

我正在尝试从Android客户端联系RESTful WCF POST Web服务,该客户端应以json格式传递一些数据。我已经成功地联系了RESTful WCF GET Web服务,但我无法弄清楚POST版本是如何工作的。

This is the android client piece of code which makes the call:

这是调用它的android客户端代码片段:

HttpPost request = new HttpPost(uri);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
/*... Building the NameValuePairs object ... */
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
/* ... handling the response ...*/

and this is the WCF web service code:

这是WCF Web服务代码:

[WebInvoke(Method = "POST",
        UriTemplate = "ServiceActivation",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    string MyPostMethod();

public string MyPostMethod()
    {
        try
        {
            /*...*/
        }
        catch (Exception e)
        {
            /*...*/
        }
    }

In this way the android client succesfully contact the web service; but I don't know how to retrieve in the MyPostMethod the data passed from the android client. Something like this: MyPostMethod(string data) ends in a bad request from the android client.

通过这种方式,android客户端成功地联系了Web服务;但我不知道如何在MyPostMethod中检索从android客户端传递的数据。像这样:MyPostMethod(字符串数据)以来自android客户端的错误请求结束。

So, what is the way to retrieve passed data in the web service?

那么,检索Web服务中传递数据的方法是什么?

Thanks in advance!

提前致谢!

3 个解决方案

#1


1  

You have to define data contract for your posted data - generally class or set of related classes which will be used to deserialize JSON message and use that class as input parameter. Here is example how to use data contracts without WCF service - it can help you to define correct contract for your message.

您必须为发布的数据定义数据协定 - 通常是类或一组相关类,这些类将用于反序列化JSON消息并将该类用作输入参数。下面是如何在没有WCF服务的情况下使用数据合同的示例 - 它可以帮助您为消息定义正确的合同。

Edit:

I just noticed that you are posting url encoded values - that is not a JSON request and it has different content type: application/x-www-form-urlencoded. JSON request for WCF is JSON passed in request content. Working with URL encoded requests in WCF is hard. This will change in Web API which has support for these requests.

我刚刚注意到你发布的是url编码值 - 这不是JSON请求,它有不同的内容类型:application / x-www-form-urlencoded。 WCF的JSON请求是在请求内容中传递的JSON。在WCF中使用URL编码的请求很难。这将在支持这些请求的Web API中发生变化。

#2


0  

Gson makes it quite easy to build json messages in Android.

Gson使得在Android中构建json消息变得非常容易。

LoginToWebSiteRequest loginToWebSiteRequest = new LoginToWebSiteRequest();// a pojo based on some json message
loginToWebSiteRequest.setEmail(email);
loginToWebSiteRequest.setPassword(password);
Gson gson = new Gson();
String json = gson.toJson(loginToWebSiteRequest,LoginToWebSiteRequest.class);
HttpPost httpPost = new HttpPost(loginUrl);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json; charset=UTF-8");

#3


0  

I'm sorry. This isn't answered for me:

对不起。我没有回答:

  1. What changes did j0nSn0w make to request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  2. j0nSn0w对request.setEntity做了哪些更改(new UrlEncodedFormEntity(nameValuePairs));

  3. If j0nSn0w thinks that 'Well, in the end it turns out to be quite easy if you know how to do it :)'; please post your solution.
  4. 如果j0nSn0w认为'嗯,最后如果你知道该怎么做就结果很容易:)';请发布您的解决方案。

I am in a similar bind to what j0nSn0w was at 17 Jun and can only find this thread as a constructive pointer.

我与j0nSn0w在6月17日的类似绑定,并且只能找到这个线程作为建设性指针。

To keep the actual solution out of the ticket seems perverse.

保持实际解决方案不在票证中似乎有悖常理。

#1


1  

You have to define data contract for your posted data - generally class or set of related classes which will be used to deserialize JSON message and use that class as input parameter. Here is example how to use data contracts without WCF service - it can help you to define correct contract for your message.

您必须为发布的数据定义数据协定 - 通常是类或一组相关类,这些类将用于反序列化JSON消息并将该类用作输入参数。下面是如何在没有WCF服务的情况下使用数据合同的示例 - 它可以帮助您为消息定义正确的合同。

Edit:

I just noticed that you are posting url encoded values - that is not a JSON request and it has different content type: application/x-www-form-urlencoded. JSON request for WCF is JSON passed in request content. Working with URL encoded requests in WCF is hard. This will change in Web API which has support for these requests.

我刚刚注意到你发布的是url编码值 - 这不是JSON请求,它有不同的内容类型:application / x-www-form-urlencoded。 WCF的JSON请求是在请求内容中传递的JSON。在WCF中使用URL编码的请求很难。这将在支持这些请求的Web API中发生变化。

#2


0  

Gson makes it quite easy to build json messages in Android.

Gson使得在Android中构建json消息变得非常容易。

LoginToWebSiteRequest loginToWebSiteRequest = new LoginToWebSiteRequest();// a pojo based on some json message
loginToWebSiteRequest.setEmail(email);
loginToWebSiteRequest.setPassword(password);
Gson gson = new Gson();
String json = gson.toJson(loginToWebSiteRequest,LoginToWebSiteRequest.class);
HttpPost httpPost = new HttpPost(loginUrl);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json; charset=UTF-8");

#3


0  

I'm sorry. This isn't answered for me:

对不起。我没有回答:

  1. What changes did j0nSn0w make to request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  2. j0nSn0w对request.setEntity做了哪些更改(new UrlEncodedFormEntity(nameValuePairs));

  3. If j0nSn0w thinks that 'Well, in the end it turns out to be quite easy if you know how to do it :)'; please post your solution.
  4. 如果j0nSn0w认为'嗯,最后如果你知道该怎么做就结果很容易:)';请发布您的解决方案。

I am in a similar bind to what j0nSn0w was at 17 Jun and can only find this thread as a constructive pointer.

我与j0nSn0w在6月17日的类似绑定,并且只能找到这个线程作为建设性指针。

To keep the actual solution out of the ticket seems perverse.

保持实际解决方案不在票证中似乎有悖常理。