httpclient - android HttpGet/HttpPost parameters allways arrive as null to the server -
i'm trying send data server seems send null values, idea? idea add new customer through mobile application database hosted in server.
here's code:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_nuevo_insert); //etresponse = (edittext) findviewbyid(r.id.etresponse2); etnombre = (edittext) findviewbyid(r.id.etnombre); etapellido = (edittext) findviewbyid(r.id.etapellido); etedad = (edittext) findviewbyid(r.id.etedad); nombre = etnombre.gettext().tostring(); apellido = etapellido.gettext().tostring(); edad = etedad.gettext().tostring(); } public void insertar(view view) { // call asynctask perform network operation on separate thread // working in localhost can't put localhost in address, // must put ip address or crush new httpasynctask().execute("http://192.168.1.34/android/insertcustomer.php"); } public static string get(string url) { inputstream inputstream = null; string result = ""; try { // create httpclient httpclient httpclient = new defaulthttpclient(); // make request given url httpresponse httpresponse = httpclient.execute(new httpget(url+ "?nombre=" + nombre + "&apellido=" + apellido + "&edad="+ edad)); // receive response inputstream inputstream = httpresponse.getentity().getcontent(); // convert inputstream string if (inputstream != null) { result = convertinputstreamtostring(inputstream); } else { result = "no ha funcionat!"; } } catch (exception e) { log.d("inputstream", e.getlocalizedmessage()); } return result; } private class httpasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { return get(urls[0]); } // onpostexecute displays results of asynctask @override protected void onpostexecute(string result) { string s = ""; toast.maketext(getbasecontext(),getresources().getstring(r.string.rebut), toast.length_long).show(); jsonarray jarray; try { jarray = new jsonarray(result); (int = 0; < jarray.length(); i++) { jsonobject json = jarray.getjsonobject(i); s = s + "nom: " + json.getstring("firsname") + " " + json.getstring("lastname") + "\n" + "edat: "+ json.getint("age") + "\n\n"; } etresponse.settext(s); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
this php file:
<?php $con = mysql_connect('localhost', 'root', ''); if(!$con){ die("no se ha podido realizar la conexion: ".mysql_error()); } mysql_select_db("testdatabase", $con); $nombre = $_get['nombre']; $apellido = $_get['apellido']; $edad = $_get['edad']; print_r($nombre."-".$apellido."-".$edad); $result = mysql_query("insert customer(firsname, lastname, age) values ('$nombre', '$apellido', '$edad')"); mysql_close($con); ?>
if getting null value means mean u r passing wrong type parameters or url may wrong check out
Comments
Post a Comment