简体   繁体   中英

Consume a WCF web service with Post Params with Android

I work with a android device which use a WCF web service.

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)

[OperationContract]
[WebInvoke(
  Method = "POST",
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json,
  UriTemplate = "interventions")]
public void SendInterventions(List<User> user)
{
}

As explained on http://debugmode.net/2011/05/15/wcf-rest-service-with-josn-data/

I used the HTTP Post code like : http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

My first try was very simple, I wanted to send a simple string post param but it doesn't work.

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();

And the .Net code

[OperationContract]
[WebInvoke(
  Method = "POST",
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json,
  UriTemplate = "interventions")]
public void SendInterventions(String user)
{
}

Does someone has a good example or somes tips ?

Regards and thanks

Edit

Well I switch on the error reporting in 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;

Edit 2

I had this error

01-22 12:28:06.830: W/System.err(2496): java.lang.IllegalStateException: Content has been consumed

I googled it and I saw that page : Using HttpClient and HttpPost in Android with post parameters

I modified the code and add httpPost.setHeader("Accept", "application/json");

and then magic it entered in the asp .net method but there is a drawback (Although the IllegalStateException stay but it is less important)

It only enter in the method when the method is :

public void SendInterventions(object user)

and not :

public void SendInterventions(String user)

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)

I tryied another 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);

same effect .. :( any idea ?

Edit 3

Well I found another very interesting page on WCF REST POST of JSON: Parameter is empty

IF I modify the WebInvoke like :

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

(The BodyStyle parameter was missing, I hope I can send complex json data with that method)

My french is not that good, but I'm guessing that you have to explicitly set the Content-type of your request. The webservice accepts json but the incoming request is raw .

Try adding application/json as the content-type in your request. Something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM