php - My Android app is not getting any response from server -
i'm beginner in android. i'm trying receive data server , check entered username , password correct or not, it's not getting response event showing "wrong match". checked json output suppose show success if entered "hello" name , "hi" password it's not showing on button click. i've added internet permission in manifest.xml
.
mainactivity.java
public class mainactivity extends appcompatactivity { private static final string register_url = "http://192.168.43.218/define_testing.php"; edittext username,pass; button login; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); login = (button) findviewbyid(r.id.login); username=(edittext)findviewbyid(r.id.username); pass=(edittext)findviewbyid(r.id.password); login.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { stringrequest request=new stringrequest(register_url, new response.listener<string>() { @override public void onresponse(string response) { try { jsonobject jso= new jsonobject(response); jsonarray array = null; array = jso.getjsonarray("result"); jsonobject obj = array.getjsonobject(0); string name= obj.getstring("name"); string password=obj.getstring("password"); string entername=username.gettext().tostring(); string enterpass=pass.gettext().tostring(); if(name.equals(entername) && password.equals(enterpass)){ toast.maketext(getapplicationcontext(),"success",toast.length_short).show(); } else { toast.maketext(getapplicationcontext(),"wrong match",toast.length_short).show(); } } catch (jsonexception e1) { e1.printstacktrace(); } }},new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { toast.maketext(getapplicationcontext(),"internet problem",toast.length_short).show(); } }); requestqueue requestqueue= volley.newrequestqueue(getapplicationcontext()); requestqueue.add(request); } } ); } }
php script
<?php require ('connection.php'); $name = 'hello'; $sql = "select * new_one_user_information username='".$name."'"; $r = mysqli_query($connect,$sql); $res = mysqli_fetch_array($r); $result = array(); array_push($result,array( "name"=>$res['username'], "password"=>$res['password'] ) ); echo json_encode(array("result"=>$result)); mysqli_close($connect); ?>
output of php script is:
connection established {"result":[{"name":"hello","password":"hi"}]}
error in logcat:
org.json.jsonexception: value connection of type java.lang.string cannot converted jsonobject @ org.json.json.typemismatch(json.java:111) @ org.json.jsonobject.<init>(jsonobject.java:159) @ org.json.jsonobject.<init>(jsonobject.java:172) @ com.example.srinu.stackview2.mainactivity.checkit(mainactivity.java:85) @ com.example.srinu.stackview2.mainactivity$1$1.onresponse(mainactivity.java:68) @ com.example.srinu.stackview2.mainactivity$1$1.onresponse(mainactivity.java:50) @ com.android.volley.toolbox.stringrequest.deliverresponse(stringrequest.java:67) @ com.android.volley.toolbox.stringrequest.deliverresponse(stringrequest.java:30) @ com.android.volley.executordelivery$responsedeliveryrunnable.run(executordelivery.java:99) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5001) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) @ dalvik.system.nativestart.main(native method)
there must "connection established" message inside required connection.php file.
and that's reason why php response not valid json.
wiki
Comments
Post a Comment