2012-01-21 9 views
1

私はWCF Webサービスを使用するアンドロイドデバイスを使用しています。Androidでポストパラメータを持つWCF Webサービスを使用する

WCFからJSONメッセージを取得してファイルを送信できましたが、WCFサービスにPOSTパラメータを送信できませんでした。私の目標はこれと同じように思うことです(オブジェクトを取得してJSONを逆シリアル化する)。私は次のようにHTTPポストコードを使用

[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://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)&#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) 

と私はそのオブジェクトに何もできない(例えばStringにキャストすることはできません)、私はユーザパラメータの名前を変更する場合、それはWORませんkのいずれかである。私は別のコードtryied

(彼はユーザパラメータを検出することができますので、私たちは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

まあ、私は同じようWebInvokeを変更する場合WCF REST POST of JSON: Parameter is empty

上の別の非常に興味深いページを見つけました:(bodyStyle属性パラメータが欠落していた

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

、私はことを願って

答えて

1

私のフランス語はそれほど良くはありませんが、私はあなたの要求のContent-typeを明示的に設定しなければならないと思います。 Webサービスはjsonを受け取りますが、着信要求はrawです。

リクエストにcontent-typeとしてapplication/jsonを追加してみてください。このようなもの:

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

ありがとうございましたが、別のエラーがありました。投稿を編集します。 –

関連する問題