Android post a JSon object to WCF receiving null -


i having problem android client trying post json class wcf service. wcf receiving post, parameter null

here code android client :

private void enviadados( dadosusuario usuario, string filename, string passunto, string pcomentario, string ponde, string pquando ) throws exception {      filename = filename.substring( filename.indexof("$$$")+3 , filename.length() );     filename = filename.substring( 0, filename.indexof("$$$") );      httpclient httpclient = new defaulthttpclient();     string url = "http://192.168.1.112/wcf2/uploadservice.svc/rest/arqinfoandroid";      string pnome = usuario.retornadados()[0];     string pddd = usuario.retornadados()[1];     string ptelefone = usuario.retornadados()[2];     string pemail = usuario.retornadados()[3];     string pregiao = usuario.retornadados()[4];      try {          uri uri = new uri(url);          httppost postrequest = new httppost(uri);          stringentity se = new stringentity(retornajson(pnome, pemail, pddd,ptelefone, pregiao, filename, pcomentario, ponde, passunto, pquando).tostring());          postrequest.setentity(se);          postrequest.setheader("accept", "application/json");         postrequest.setheader("content-type", "application/json;");          httpresponse response = httpclient.execute(postrequest);          int stacode = response.getstatusline().getstatuscode();           if (stacode == 200 || stacode == 400) {              inputstream instream = response.getentity().getcontent();                 string res = convertstreamtostring(instream);                  if(res.contains(("falha")))                 {                     throw new exception("falha ao enviar arquivo");                  }          }       } catch (unsupportedencodingexception e) {     } catch (clientprotocolexception e) {     } catch (ioexception e) {     } catch (urisyntaxexception e){      }   }   public  jsonstringer retornajson(string nome, string email, string ddd, string telefone, string regiao, string arquivo, string comentario, string onde, string assunto, string quando) {      jsonstringer dadosusu = new jsonstringer();      try      {           dadosusu.object()                  .key("dadosusuario").object().key("nome")                  .value(nome).key("email")                  .value(email).key("ddd")                  .value(ddd).key("telefone")                  .value(telefone).key("regiao")                  .value(regiao).key("nomearq")                  .value(arquivo).key("comentario")                  .value(comentario).key("onde")                  .value(onde).key("assunto")                  .value(assunto).key("quando")                  .value(quando).endobject().endobject();       }      catch (exception je)      {                                                                                                                                                                                                                                                                                                                                 }         return dadosusu; } 

here code wcf (the parameter dadosusuario null)

[webinvoke(method = "post", requestformat = webmessageformat.json, responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.wrappedrequest, uritemplate = "arqinfoandroid")]     public string arqinfoandroid(dadosusu dadosusuario)     {          escrevernolog("recebendo dados usuário android");          try         {              string absfilename = string.format("{0}\\fileupload\\{1}"                                     , appdomain.currentdomain.basedirectory                                     , dadosusuario.nomearq);               gerenciathread pth = new gerenciathread();               escrevernolog("recebendo dados usuário android: " +                             dadosusuario.nome + " - " +                             dadosusuario.email + " - " +                             dadosusuario.ddd + " - " +                             dadosusuario.telefone + " - " +                             dadosusuario.regiao + " - " +                             absfilename + " - " +                             dadosusuario.comentario + " - " +                             dadosusuario.onde + " - " +                             dadosusuario.assunto + " - " +                             datetime.parseexact(dadosusuario.quando, "dd/mm/yyyy", null));             pth.chamarthread(dadosusuario.nome, dadosusuario.email, dadosusuario.ddd, dadosusuario.telefone, dadosusuario.regiao, absfilename, dadosusuario.comentario, dadosusuario.onde, dadosusuario.assunto, datetime.parseexact(dadosusuario.quando, "dd/mm/yyyy", null));              return "ok";         }         catch (exception ex)         {             escrevernolog("erro recebimento dos dados usuário android");             escrevernolog("mensagem de erro recebimento dos dados usuário android: " + ex.message);             return "falha: " + ex.message;         }        }      public class dadosusu     {         private string nome;         private string email;         private string ddd;         private string telefone;         private string regiao;         private string nomearq;         private string comentario;         private string onde;         private string assunto;         private string quando;          [datamember(name="nome")]         public string nome         {             { return nome; }             set { nome = value; }         }          [datamember(name="email")]         public string email         {             { return email; }             set { email = value; }         }          [datamember(name="ddd")]         public string ddd         {             { return ddd; }             set { ddd = value; }         }          [datamember(name="telefone")]         public string telefone         {             { return telefone; }             set { telefone = value; }         }          [datamember(name="regiao")]         public string regiao         {             { return regiao; }             set { regiao = value; }         }          [datamember(name="nomearq")]         public string nomearq         {             { return nomearq; }             set { nomearq = value; }         }           [datamember(name="comentario")]         public string comentario         {             { return comentario; }             set { comentario = value; }         }           [datamember(name="onde")]         public string onde         {             { return onde; }             set { onde = value; }         }          [datamember(name="assunto")]         public string assunto         {             { return assunto; }             set { assunto = value; }         }          [datamember(name="quando")]         public string quando         {             { return quando; }             set { quando =  value; }         }       } 

the parameter name in operation (with body style wrappedrequest) dadosusuario. in json you're creating in android code, name of property dadosusuario. case important - try changing dadosusuario (or changing parameter name in operation have first character in upper case), , should work.

on unrelated note - don't need specify name property on [datamember] attributes since they're same name properties of class. doesn't hurt, though.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -