json - How to send String using HttpURLConnection in android -


i need send string web service , have doubts how send string using httpurlconnection.

obs: in string "result" have like:

 {"sex":"famale","nome":"larissa aparecida nogueira","convenios":[{"convenio":2,"tipo":"principal","number":"44551-1456-6678-3344"}],"user":"lari.ap","email":"lari.ap@yahoo.com.br","cell":"(19)98167-5569"} 

following code:

   public usuerservice(context context, string result) {        this.progressdialog = new progressdialog(context);        this.context = context;        this.result = result;    }    @override    protected string doinbackground(string... params) {          string responsestring = "";         try {              url url = new url(constants.usuario + "/createusuario");              httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection();              httpurlconnection.setrequestmethod("post");                bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(httpurlconnection.getinputstream()));              string inputline;              stringbuilder response = new stringbuilder();               while ((inputline = bufferedreader.readline()) != null) {              response.append(inputline);            }              result = response.tostring();             bufferedreader.close();            } catch (exception e) {             log.d("inputstream", e.getmessage());         }           return null;    } 

i have class picks data , parses jsonobject. need understand how send object.tostring() web service using httpurlconnection.

following code:

 public string parserusuariojson(){      jsonobject object = new jsonobject();      try {         object.put(constants.key_name, musuario.getnome());         object.put(constants.key_email, musuario.getemail());         object.put(constants.key_user, musuario.getuser());         object.put(constants.key_pass, musuario.getsenha());         object.put(constants.key_sex, musuario.getsexo());         object.put(constants.key_cellphone, musuario.getcelular());          jsonarray array = new jsonarray();          for(int = 0; < musuario.getusuarioconvenios().size() ; i++){             jsonobject convenio = new jsonobject();              convenio.put(constants.key_convenio, musuario.getusuarioconvenios().get(i).getconvenio().getid());             convenio.put(constants.key_number, musuario.getusuarioconvenios().get(i).getnumero());             convenio.put(constants.key_type, musuario.getusuarioconvenios().get(i).gettipo());              array.put(convenio);         }         object.put(constants.key_convenios, array);     } catch (jsonexception e) {         log.e("register", e.getmessage());     }      return object.tostring();  } 

thanks in advance. :)

use namevaluepairlist send data.

try this...

httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(constants.usuario + "/createusuario");  try {          // add key-value pair here         list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>();         namevaluepairs.add(new basicnamevaluepair("sex", "female"));         namevaluepairs.add(new basicnamevaluepair("nome", "larissa aparecida nogueira"));         // set other key-value pairs          httppost.setentity(new urlencodedformentity(namevaluepairs));          httpresponse response = httpclient.execute(httppost);      } catch (clientprotocolexception e) {         // todo auto-generated catch block      } catch (ioexception e) {         // todo auto-generated catch block      }  

for sending json object on network using http post.

pass json string here

 stringentity se = new stringentity(object.tostring());  httpost.setentity(se);  httpost.setheader("accept", "application/json");  httpost.setheader("content-type", "application/json");  httpresponse response = httpclient.execute(httpost); 

don't forget catch exception.

sending json object using httpurlconnection...

try {   //constants   url url = new url(constants.usuario + "/createusuario");   string yourjsonstring = object.tostring();    httpurlconnection conn = (httpurlconnection) url.openconnection();   conn.setrequestmethod("post");   conn.setdoinput(true);   conn.setdooutput(true);   conn.setfixedlengthstreamingmode(yourjsonstring.getbytes().length);    conn.setrequestproperty("content-type", "application/json;charset=utf-8");   conn.setrequestproperty("x-requested-with", "xmlhttprequest");    conn.connect();    outputstream os = new bufferedoutputstream(conn.getoutputstream());   os.write(yourjsonstring.getbytes());    os.flush();    = conn.getinputstream(); } {     //clean   os.close();   is.close();   conn.disconnect(); } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -