我使用的是使用WCF Web服務的Android設備。使用Post Params與Android一起使用WCF Web服務
我設法從WCF獲取JSON消息,發送文件,但我沒有設法發送POST PARAMS到WCF服務。我的目標是有這樣的想法(或者得到一個對象,然後反序列化JSON)。 (用戶基本上是字符串和整數組成的一個複雜的對象)
[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郵編: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
我GOOGLE了它,我看到該頁面:Using HttpClient and HttpPost in Android with post parameters
我修改了代碼,並添加httpPost.setHeader( 「接受」,「application/json」);
,然後魔術就在ASP.NET的方法進入,但有一個缺點(雖然IllegalStateException異常逗留,但它是不那麼重要)
當方法是隻在輸入方法:
public void SendInterventions(object user)
,而不是:
public void SendInterventions(String user)
,我可以做那個對象沒有(不能被強制轉換爲字符串爲例),如果我重新命名用戶參數不會活像k也是。 (所以我們可以承認JSON查詢解析,因爲他可以檢測用戶參數)
我tryied另一個代碼:
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數據)
謝謝你這是,但我有另一個錯誤..我編輯帖子 –