java - Inserting data into online server using mysql, json in android. -
i used this example insert data in table in online server. may post duplicate unable figure out going wrong in code. saw many posts here , modified mine accordingly still errors. kindly me.
inser.php
<?php $response = array(); $connect = mysqli_connect(" "," "," "," "); if(mysqli_connect_errno($connect)) { echo "failed connect mysql: " . mysqli_connect_error(); } else { echo "success"; } $make = isset($_post['make']) ? $_post['make'] : ''; $model = isset($_post['model']) ? $_post['model'] : ''; $regno=isset($_post['regno']) ? $_post['regno'] : ''; $engine=isset($_post['engine']) ? $_post['engine']: ''; $chasis=isset($_post['chasis']) ? $_post['chasis']: ''; $query = mysqli_query($connect, "insert car (make, model,regno,engine,chasis) values ('$make','$model','$regno','$engine','$chasis') "); //$query = mysqli_query($connect, "insert car (make, model,regno,engine,chasis) values('maruthi' ,'ma22','434','42343','32423') "); // check if row inserted or not if ($query) { // inserted database $response["success"] = 1; $response["message"] = "product created."; // echoing json response echo json_encode($response); } else { // failed insert row $response["success"] = 0; $response["message"] = "oops! error occurred."; // echoing json response echo json_encode($response); } mysqli_close($connect); ?>
mainactivity.java
import org.json.jsonarray; import org.json.jsonexception; import com.example.cardetails.customhttpclient; public class mainactivity extends activity { customhttpclient jsonparser = new customhttpclient(); private progressdialog pdialog; string[] make1 = {"general motors","audi","ford","maruthi suzuki","toyota","travelz"}; public edittext chasis; public edittext engno; public edittext regno; public edittext model; public edittext textview; public jsonobject json; private static final string tag_success = "success"; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); model=(edittext)findviewbyid(r.id.model); regno=(edittext)findviewbyid(r.id.registerno); engno=(edittext)findviewbyid(r.id.engine); chasis=(edittext)findviewbyid(r.id.chasis); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_dropdown_item_1line,make1); final autocompletetextview textview= (autocompletetextview)findviewbyid(r.id.ap); textview.setthreshold(1); textview.setadapter(adapter); button save=(button)findviewbyid(r.id.save); button cancel=(button)findviewbyid(r.id.cancel); save.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { new createnewproduct().execute(); } });} class createnewproduct extends asynctask<string, string, string> { protected void onpreexecute() { super.onpreexecute(); progressdialog pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("adding details.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected string doinbackground(string... args) { string make = textview.gettext().tostring(); string model = model.gettext().tostring(); string regno= regno.gettext().tostring(); string engine=engno.gettext().tostring(); string chasis=chasis.gettext().tostring(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("make",make )); params.add(new basicnamevaluepair("model",model)); params.add(new basicnamevaluepair("regno",regno)); params.add(new basicnamevaluepair("engine",engine)); params.add(new basicnamevaluepair("chasis",chasis)); // getting json object // note create product url accepts post method int success=0; jsonobject json = customhttpclient.makehttprequest( "http://***.com/db/insert.php", "post", params); log.d("create response", json.tostring()); // check success tag try { success = json.getint(tag_success); } catch (jsonexception e) { e.printstacktrace(); success=0; } return ""+success; } protected void onpostexecute(string result) { pdialog.dismiss(); if (integer.parseint(result) == 1) { intent = new intent( mainactivity.this,createtripactivity.class); startactivity(i); finish(); } else { //your task here } } } }
and logcat
03-24 03:51:16.124: d/(1451): hostconnection::get() new host connection established 0xb7569238, 03-24 03:51:16.374: w/egl_emulation(1451): eglsurfaceattrib not implemented 03-24 03:51:16.404: d/openglrenderer(1451): enabling debug mode 0 03-24 03:51:20.374: d/dalvikvm(1451): gc_for_alloc freed 112k, 6% free 3153k/3348k, paused 57ms, 03-24 03:51:20.494: d/dalvikvm(1451): gc_for_alloc freed 6k, 6% free 3202k/3404k, paused 112ms, 03-24 03:51:20.494: i/dalvikvm-heap(1451): grow heap (frag case) 3.810mb 635812-byte 03-24 03:51:20.614: d/dalvikvm(1451): gc_for_alloc freed 2k, 6% free 3821k/4028k, paused 118ms, 03-24 03:51:20.834: w/egl_emulation(1451): eglsurfaceattrib not implemented 03-24 03:51:28.834: w/dalvikvm(1451): threadid=12: thread exiting uncaught exception (group=0xb3b08ba8) 03-24 03:51:28.874: e/androidruntime(1451): fatal exception: asynctask #2 03-24 03:51:28.874: e/androidruntime(1451): process: com.example.cardetails, pid: 1451 03-24 03:51:28.874: e/androidruntime(1451): java.lang.runtimeexception: error occured while executing doinbackground() 03-24 03:51:28.874: e/androidruntime(1451): @ android.os.asynctask$3.done(asynctask.java:300) 03-24 03:51:28.874: e/androidruntime(1451): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) 03-24 03:51:28.874: e/androidruntime(1451): @ java.util.concurrent.futuretask.setexception(futuretask.java:222) 03-24 03:51:28.874: e/androidruntime(1451): @ java.util.concurrent.futuretask.run(futuretask.java:242) 03-24 03:51:28.874: e/androidruntime(1451): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 03-24 03:51:28.874: e/androidruntime(1451): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 03-24 03:51:28.874: e/androidruntime(1451): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 03-24 03:51:28.874: e/androidruntime(1451): @ java.lang.thread.run(thread.java:841) 03-24 03:51:28.874: e/androidruntime(1451): caused by: java.lang.nullpointerexception 03-24 03:51:28.874: e/androidruntime(1451): @ com.example.cardetails.mainactivity$createnewproduct.doinbackground(mainactivity.java:73) 03-24 03:51:28.874: e/androidruntime(1451): @ com.example.cardetails.mainactivity$createnewproduct.doinbackground(mainactivity.java:1) 03-24 03:51:28.874: e/androidruntime(1451): @ android.os.asynctask$2.call(asynctask.java:288) 03-24 03:51:28.874: e/androidruntime(1451): @ java.util.concurrent.futuretask.run(futuretask.java:237) 03-24 03:51:28.874: e/androidruntime(1451): ... 4 more 03-24 03:51:28.914: w/egl_emulation(1451): eglsurfaceattrib not implemented 03-24 03:51:30.634: i/choreographer(1451): skipped 31 frames! application may doing work on main thread. 03-24 03:51:31.814: i/process(1451): sending signal. pid: 1451 sig: 9
edit: customhttpclient.java
public class customhttpclient { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public customhttpclient() { } // function json url // making http post or mehtod public static jsonobject makehttprequest(string url,string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); }else if(method == "get"){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
you need change
if(method == "post"){
to
if(method.equals("post")){
similarly get
change
else if(method == "get"){
to
else if(method.equals("get")){
when compare strings need use .equals
or .equlasignorecase
edit:
what string , hence not jsonobject
secondly change this
protected void onpreexecute() { super.onpreexecute(); progressdialog pdialog = new progressdialog(mainactivity.this);
to
protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this);
Comments
Post a Comment