简体   繁体   English

使用Android的Post Params消费WCF Web服务

[英]Consume a WCF web service with Post Params with Android

I work with a android device which use a WCF web service. 我使用使用WCF网络服务的Android设备。

I managed to get a JSON message from WCF, send a file but I didn't manage to send POST params to the WCF service. 我设法从WCF获取JSON消息,发送文件,但没有设法将POST参数发送到WCF服务。 My goal is to have somethink like this (Or get an object and then deserialize the JSON). 我的目标是进行这样的思考(或获取一个对象,然后反序列化JSON)。 (User is a complex object composed of basically String and Integer) (用户是一个复杂的对象,基本上由String和Integer组成)

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

As explained on http://debugmode.net/2011/05/15/wcf-rest-service-with-josn-data/ http://debugmode.net/2011/05/15/wcf-rest-service-with-josn-data/所述

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

My first try was very simple, I wanted to send a simple string post param but it doesn't work. 我的第一次尝试非常简单,我想发送一个简单的字符串后参数,但是它不起作用。

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();

And the .Net code 和.Net代码

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

Does someone has a good example or somes tips ? 有人有很好的榜样或提示吗?

Regards and thanks 问候和感谢

Edit 编辑

Well I switch on the error reporting in asp .net wcf. 好吧,我在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;

Edit 2 编辑2

I had this error 我有这个错误

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

I googled it and I saw that page : Using HttpClient and HttpPost in Android with post parameters 我用谷歌搜索,然后看到了该页面: 在带有post参数的Android中使用HttpClient和HttpPost

I modified the code and add httpPost.setHeader("Accept", "application/json"); 我修改了代码,并添加了httpPost.setHeader(“ Accept”,“ application / json”);

and then magic it entered in the asp .net method but there is a drawback (Although the IllegalStateException stay but it is less important) 然后魔术它输入到asp .net方法中,但是有一个缺点(尽管保留了IllegalStateException,但是它不太重要)

It only enter in the method when the method is : 仅当方法为时才输入该方法:

public void SendInterventions(object user)

and not : 并不是 :

public void SendInterventions(String user)

and I can do nothing with that object (can't be casted to String for example) and if I rename the user parameter it won't work either. 而且我无法对该对象执行任何操作(例如,无法将其强制转换为String),并且如果我重命名用户参数,它也将无法工作。 (so we can admit that the json query is parsed because he can detect the user parameter) (因此我们可以承认已解析json查询,因为他可以检测到用户参数)

I tryied another code : 我尝试了另一个代码:

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

same effect .. :( any idea ? 同样的效果.. :(有什么想法吗?

Edit 3 编辑3

Well I found another very interesting page on WCF REST POST of JSON: Parameter is empty 我在JSON的WCF REST POST上发现了另一个非常有趣的页面:参数为空

IF I modify the WebInvoke like : 如果我像这样修改WebInvoke:

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

(The BodyStyle parameter was missing, I hope I can send complex json data with that method) (缺少BodyStyle参数,我希望可以使用该方法发送复杂的json数据)

My french is not that good, but I'm guessing that you have to explicitly set the Content-type of your request. 我的法语不太好,但是我猜测您必须显式设置请求的Content-type The webservice accepts json but the incoming request is raw . Web服务接受json但传入的请求是raw

Try adding application/json as the content-type in your request. 尝试在您的请求中添加application/json作为内容类型。 Something like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM