【问题标题】:Consume a WCF web service with Post Params with Android在 Android 中使用带有 Post Params 的 WCF Web 服务
【发布时间】:2012-01-21 18:35:33
【问题描述】:

我使用使用 WCF Web 服务的 android 设备。

我设法从 WCF 获取 JSON 消息,发送一个文件,但我没有设法将 POST 参数发送到 WCF 服务。我的目标是有这样的想法(或者获取一个对象,然后反序列化 JSON)。 (用户是一个基本由String和Integer组成的复杂对象)

[OperationContract]
[WebInvoke(
  Method = "POST",
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json,
  UriTemplate = "interventions")]
public void SendInterventions(List<User> user)
{
}

如http://debugmode.net/2011/05/15/wcf-rest-service-with-josn-data/ 中所述

我使用了 HTTP Post 代码,例如:http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

我的第一次尝试很简单,我想发送一个简单的字符串 post 参数但它不起作用。

HttpClient client = new DefaultHttpClient();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "kris"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);

HttpPost post = new HttpPost(url);
post.setEntity(ent);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();

还有 .Net 代码

[OperationContract]
[WebInvoke(
  Method = "POST",
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json,
  UriTemplate = "interventions")]
public void SendInterventions(String user)
{
}

有人有很好的例子或一些提示吗?

问候和感谢

编辑

我在 asp .net wcf 中打开错误报告。

01-22 11:49:46.800: D/SendInterventions(2049):
<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
<Code><Value>Receiver</Value>
<Subcode><Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</Value></Subcode>
</Code><Reason>
<Text xml:lang="fr-FR">Le message entrant a un format inattendu 'Raw'. Les formats de message attendus pour l'opération sont 'Xml'; 'Json'. Un WebContentTypeMapper n'a peut-être pas été configuré sur la liaison. Pour plus d'informations, consultez la documentation relative à WebContentTypeMapper.</Text>
</Reason><Detail><ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HelpLink i:nil="true"/><InnerException i:nil="true"/>
<Message>Le message entrant a un format inattendu 'Raw'. Les formats de message attendus pour l'opération sont 'Xml'; 'Json'. Un WebContentTypeMapper n'a peut-être pas été configuré sur la liaison. Pour plus d'informations, consultez la documentation relative à WebContentTypeMapper.</Message>
<StackTrace>   à System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)&#xD;

编辑 2

我遇到了这个错误

01-22 12:28:06.830: W/System.err(2496): java.lang.IllegalStateException: Content has been consumed

我用谷歌搜索了一下,我看到了那个页面:Using HttpClient and HttpPost in Android with post parameters

我修改了代码,添加了httpPost.setHeader("Accept", "application/json");

然后魔法它进入asp.net方法但是有一个缺点(虽然IllegalStateException留了但是没那么重要)

只有在方法为:时才进入方法:

public void SendInterventions(object user)

而不是:

public void SendInterventions(String user)

我对该对象无能为力(例如,不能转换为字符串),如果我重命名用户参数,它也将不起作用。 (所以我们可以承认json查询被解析,因为他可以检测到用户参数)

我尝试了另一个代码:

JSONObject jsonObj = new JSONObject();
jsonObj.put("user", "test");
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
post.setEntity(entity);

同样的效果.. :(有什么想法吗?

编辑 3

我在WCF REST POST of JSON: Parameter is empty上发现了另一个非常有趣的页面

如果我像这样修改 WebInvoke:

[WebInvoke(
  RequestFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.WrappedRequest,
  UriTemplate = "interventions")]

(缺少BodyStyle参数,希望能用那个方法发送复杂的json数据)

【问题讨论】:

    标签: android asp.net wcf json web-services


    【解决方案1】:

    我的法语不是那么好,但我猜你必须明确设置你的请求的Content-type。网络服务接受json,但传入的请求是raw。

    尝试在您的请求中添加 application/json 作为内容类型。像这样的:

    post.setHeader("Content-Type", "application/json");
    

    【讨论】:

    • 谢谢你,但我有另一个错误..我编辑帖子
    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多