[英]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.