title: Consume a WCF web service with Post Params with Android
body: <p>I work with a android device which use a WCF web service.</p>
  
  <p>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. My goal is to have somethink like this (Or get an object and then deserialize the JSON). (User is a complex object composed of basically String and Integer)</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(
    Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "interventions")]
  public void SendInterventions(List&lt;User&gt; user)
  {
  }
  </code></pre>
  
  <p>As explained on <a href="http://debugmode.net/2011/05/15/wcf-rest-service-with-josn-data/" rel="nofollow noreferrer">http://debugmode.net/2011/05/15/wcf-rest-service-with-josn-data/</a></p>
  
  <p>I used the HTTP Post code like : <a href="http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/" rel="nofollow noreferrer">http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/</a></p>
  
  <p>My first try was very simple, I wanted to send a simple string post param but it doesn't work.</p>
  
  <pre><code>HttpClient client = new DefaultHttpClient();
  List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
  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();
  </code></pre>
  
  <p>And the .Net code</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(
    Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "interventions")]
  public void SendInterventions(String user)
  {
  }
  </code></pre>
  
  <p>Does someone has a good example or somes tips ?</p>
  
  <p>Regards and thanks</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Well I switch on the error reporting in asp .net wcf.</p>
  
  <pre><code>01-22 11:49:46.800: D/SendInterventions(2049):
  &lt;Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none"&gt;
  &lt;Code&gt;&lt;Value&gt;Receiver&lt;/Value&gt;
  &lt;Subcode&gt;&lt;Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher"&gt;a:InternalServiceFault&lt;/Value&gt;&lt;/Subcode&gt;
  &lt;/Code&gt;&lt;Reason&gt;
  &lt;Text xml:lang="fr-FR"&gt;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.&lt;/Text&gt;
  &lt;/Reason&gt;&lt;Detail&gt;&lt;ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt;HelpLink i:nil="true"/&gt;&lt;InnerException i:nil="true"/&gt;
  &lt;Message&gt;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.&lt;/Message&gt;
  &lt;StackTrace&gt;   à System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)&amp;#xD;
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>I had this error </p>
  
  <pre><code>01-22 12:28:06.830: W/System.err(2496): java.lang.IllegalStateException: Content has been consumed
  </code></pre>
  
  <p>I googled it and I saw that page : <a href="https://stackoverflow.com/questions/6028981/using-httpclient-and-httppost-in-android-with-post-parameters">Using HttpClient and HttpPost in Android with post parameters</a></p>
  
  <p>I modified the code and add httpPost.setHeader("Accept", "application/json");</p>
  
  <p>and then magic it entered in the asp .net method but there is a drawback (Although the IllegalStateException stay but it is less important)</p>
  
  <p>It only enter in the method when the method is :</p>
  
  <pre><code>public void SendInterventions(object user)
  </code></pre>
  
  <p>and not :</p>
  
  <pre><code>public void SendInterventions(String user)
  </code></pre>
  
  <p>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. (so we can admit that the json query is parsed because he can detect the user parameter)</p>
  
  <p>I tryied another code :</p>
  
  <pre><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);
  </code></pre>
  
  <p>same effect .. :( any idea ?</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>Well I found another very interesting page on <a href="https://stackoverflow.com/questions/6835872/wcf-rest-post-of-json-parameter-is-empty">WCF REST POST of JSON: Parameter is empty</a></p>
  
  <p>IF I modify the WebInvoke like :</p>
  
  <pre><code>[WebInvoke(
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    UriTemplate = "interventions")]
  </code></pre>
  
  <p>(The BodyStyle parameter was missing, I hope I can send complex json data with that method)</p>