[英]Consume a WCF web service with Post Params with Android
我使用使用WCF网络服务的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/
我的第一次尝试非常简单,我想发送一个简单的字符串后参数,但是它不起作用。
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)
编辑2
我有这个错误
01-22 12:28:06.830: W/System.err(2496): java.lang.IllegalStateException: Content has been consumed
我用谷歌搜索,然后看到了该页面: 在带有post参数的Android中使用HttpClient和HttpPost
我修改了代码,并添加了httpPost.setHeader(“ Accept”,“ application / json”);
然后魔术它输入到asp .net方法中,但是有一个缺点(尽管保留了IllegalStateException,但是它不太重要)
仅当方法为时才输入该方法:
public void SendInterventions(object user)
并不是 :
public void SendInterventions(String user)
而且我无法对该对象执行任何操作(例如,无法将其强制转换为String),并且如果我重命名用户参数,它也将无法工作。 (因此我们可以承认已解析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
我在JSON的WCF REST POST上发现了另一个非常有趣的页面:参数为空
如果我像这样修改WebInvoke:
[WebInvoke(
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "interventions")]
(缺少BodyStyle参数,我希望可以使用该方法发送复杂的json数据)
我的法语不太好,但是我猜测您必须显式设置请求的Content-type
。 Web服务接受json
但传入的请求是raw
。
尝试在您的请求中添加application/json
作为内容类型。 像这样:
post.setHeader("Content-Type", "application/json");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.