php - json - Value <br of type java.lang.String cannot be converted to JSONObject -
i trying log in on app, access online database , check if inputs registered i'm getting error: value < br of type java.lang.string cannot converted jsonobject .
i don't know why php code returns < br />. (i added space between < , br show)
here's code.
import android.app.activity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.os.asynctask; import android.support.v7.app.actionbar; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.widget.button; import android.widget.edittext; import android.view.view; import android.widget.toast; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; public class login extends activity { button btnlogin; intent intent; jsonobject jsonobject; button btnsignup; edittext txtusername, txtpassword; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); btnlogin = (button)findviewbyid(r.id.btnlogin); btnsignup = (button)findviewbyid(r.id.btnsignup); txtusername = (edittext)findviewbyid(r.id.txtliusername); txtpassword = (edittext)findviewbyid(r.id.txtlipassword); // jsonobject = jsonfunctions.getjsonfromurl("http://sql18.hostinger.ph/phpmyadmin/index.php?db=u897407316_tret&lang=en&token=6afd355a23affd65cb4d05f814cc7921&phpmyadmin=e2ba129883c7c52bc7e30fb543d3fa1095372c90"); } public void gosignup(view view){ intent = new intent(this, signup.class); startactivity(intent); } public void gologin(view view){ string name = txtusername.gettext().tostring(); string pw = txtpassword.gettext().tostring(); string type = "login"; if((name.trim().equals(""))||pw.trim().equals("")){ toast.maketext(getapplicationcontext(), "please fill information", toast.length_long).show(); } else { toast.maketext(this, "logging in...", toast.length_short).show(); new loginaction(this).execute(name, pw); // intent = new intent(this, userhomedrawer.class); //startactivity(intent); } } }
loginaction.java
import android.content.context; import android.content.intent; import android.os.asynctask; import android.util.log; import android.widget.toast; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlencoder; /** * created camille on 11/13/2016. */ public class loginaction extends asynctask<string, void, string> { private context context; public loginaction(context context) { this.context = context; } protected void onpreexecute() { } @override protected string doinbackground(string... arg0) { string username = arg0[0]; string password = arg0[1]; string link; string data; bufferedreader bufferedreader; string result; try { data = "?username=" + urlencoder.encode(username, "utf-8"); data += "&password=" + urlencoder.encode(password, "utf-8"); link = "http://threatmam.esy.es/loginactionandroid.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.tostring(); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // jsonobject jsonobj = new jsonobject(jsonstr.substring(jsonstr.indexof("{"), jsonstr.lastindexof("}") + 1)); string query_result = jsonobj.getstring("query_result"); if (query_result.equals("success")) { toast.maketext(context, "log in successful!", toast.length_short).show(); intent intent = new intent(context,userhomedrawer.class) .setflags(intent.flag_activity_new_task); context.startactivity(intent); } else if (query_result.equals("failure")) { toast.maketext(context, "log in failed.", toast.length_short).show(); } else { toast.maketext(context, "couldn't connect remote database.", toast.length_short).show(); } } catch (jsonexception e) { e.printstacktrace(); log.d("json", "error:",e); toast.maketext(context, ""+e+"res: "+jsonstr, toast.length_short).show(); } } else { toast.maketext(context, "couldn't json data.", toast.length_short).show(); } } }
and here's php code
<?php $un = $_get['username']; $pw = md5(sha1($_get['password'])); $res = 0; include "connect.php"; $query = "select * tbl_mobileuser"; result = $mysqli->query($query); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { if(($un == $row['username']) && if($pw == $row['password'])){ echo '{"query_result":"success"}'; $res = 1; } } } else{ echo '{"query_result":"failure"}'; } if($res == 0){ echo '{"query_result":"failure"}'; } ?>
i looked possible solutions still can't solve this. hope can me.
looks missed $
on line#11.
change,
result = $mysqli->query($query);
to,
$result = $mysqli->query($query);
note : not reading response php script correctly. readline()
reads single line. if php script outputs multiple (as now), android code wont able read , miss data.
Comments
Post a Comment