php - android apps adding data to mysql database throws JSONException -


i'm trying add row in table person(nom,pass_world) in mysql database using android apps. when execute php code (savedata.php) in browser shows me : query_result":"success", means row successefly added. search in google code allows android app add data mysql database tryed it, unfortunately throws jsoneexcption , shows message "error parsing json data" can see in code below:

savedata.php

<?php  $con=mysqli_connect("localhost","root","","othmane");  if (mysqli_connect_errno($con))  {     echo '{"query_result":"error"}';  }    $name = $_get['nom'];  $pass = $_get['pass_world'];     $result = mysqli_query($con,"insert person (nom,pass_world) values ('$name', '$pass')");     if($result == true) {      echo '{"query_result":"success"}';  }  else{      echo '{"query_result":"failure"}';  }  mysqli_close($con);  ?>

mainactivity.java

public class mainactivity extends appcompatactivity { edittext inputname; edittext inputpwd; button button; string nom, password;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     inputname = (edittext) (findviewbyid(r.id.edittext));     inputpwd = (edittext) (findviewbyid(r.id.edittext2));     button = (button) findviewbyid(r.id.button); }  public void save(view view) {     nom = inputname.gettext().tostring();     password = inputpwd.gettext().tostring();      new signupactivity(this).execute(nom, password); } 

}

signupativity.java

public class signupactivity extends asynctask<string, void, string> {  private context context;  public signupactivity(context context) {     this.context = context; }  protected void onpreexecute() {  }  @override protected string doinbackground(string... params) {     string link;     string data;     bufferedreader bufferedreader;     string result;      try {         data = "?nom=jean";         data += "&pass_world=123456";          link = "http://localhost/save/savedata.php" + data;         url url = new url(link);         httpurlconnection con = (httpurlconnection) url.openconnection();          bufferedreader = new bufferedreader(new inputstreamreader(con.getinputstream()));         result = bufferedreader.readline();         return result;     } catch (exception e) {         return new string("exception: " + e.getmessage());     } } @override protected void onpostexecute(string result) {     string jsonstr = result;     if (jsonstr != null) {         try {             jsonobject jsonobj = new jsonobject(jsonstr);             string query_result = jsonobj.getstring("query_result");             if (query_result.equals("success")) {                 toast.maketext(context, "data inserted successfully. signup successfull.", toast.length_short).show();             } else if (query_result.equals("failure")) {                 toast.maketext(context, "data not inserted. signup failed.", toast.length_short).show();             } else {                 toast.maketext(context, "couldn't connect remote database.", toast.length_short).show();             }         } catch (jsonexception e) {             e.printstacktrace();             toast.maketext(context,"error parsing json data.", toast.length_short).show();         }     } else {         toast.maketext(context, "couldn't json data.", toast.length_short).show();     } } 

}

log cat :

03-07 06:42:26.992 8503-8503/com.example.othmane.myapplication d/!!: exception: failed connect localhost/127.0.0.1 (port 80): connect failed: econnrefused (connection refused) 03-07 06:42:29.844 8503-8503/com.example.othmane.myapplication w/system.err: org.json.jsonexception: value exception of type java.lang.string cannot converted jsonobject 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ org.json.json.typemismatch(json.java:111) 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ org.json.jsonobject.(jsonobject.java:160) 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ org.json.jsonobject.(jsonobject.java:173) 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ com.example.othmane.myapplication.signupactivity.onpostexecute(signupactivity.java:60) 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ com.example.othmane.myapplication.signupactivity.onpostexecute(signupactivity.java:20) 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ android.os.asynctask.finish(asynctask.java:651) 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ android.os.asynctask.-wrap1(asynctask.java) 03-07 06:42:29.845 8503-8503/com.example.othmane.myapplication w/system.err: @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:668) 03-07 06:42:29.846 8503-8503/com.example.othmane.myapplication w/system.err: @ android.os.handler.dispatchmessage(handler.java:102) 03-07 06:42:29.847 8503-8503/com.example.othmane.myapplication w/system.err: @ android.os.looper.loop(looper.java:148) 03-07 06:42:29.847 8503-8503/com.example.othmane.myapplication w/system.err: @ android.app.activitythread.main(activitythread.java:5417) 03-07 06:42:29.847 8503-8503/com.example.othmane.myapplication w/system.err: @ java.lang.reflect.method.invoke(native method) 03-07 06:42:29.847 8503-8503/com.example.othmane.myapplication w/system.err: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) 03-07 06:42:29.847 8503-8503/com.example.othmane.myapplication w/system.err: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 03-07 06:42:29.847 8503-8503/com.example.othmane.myapplication d/!!: exception: failed connect localhost/127.0.0.1 (port 80): connect failed: econnrefused (connection refused)

you have mention ip address of server providing post service.

link = "http://localhost/save/savedata.php" + data; 

make

link = "http://<ip address : port>/save/savedata.php" + data; 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -