php - Json parsing in Login -
first of newbie in android. have read every similar topic couldn't find specific answers. problem @ json parsing of login activity. can find nullpointexception come from; im stucked on 1 week... in advance
i have mention app worked charm. problems began after trying connect localhost fare away wifi. isnt strange;
login.java
package com.example.tranfer; import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class login extends activity implements onclicklistener{ private edittext user, pass; private button msubmit, mregister; // progress dialog private progressdialog pdialog; // json parser class jsonparser jsonparser = new jsonparser(); //php login script location: //localhost : //testing on device //put local ip instead, on windows, run cmd > ipconfig //or in mac's terminal type ifconfig , ip under en0 or en1 // private static final string login_url = "http://192.168.1.4:80/etruck1/login1.php"; //testing on emulator: private static final string login_url = "http://............................."; //testing real server: //private static final string login_url = "http://www.yourdomain.com/webservice/login.php"; //json element ids repsonse of php script: private static final string tag_success = "success"; private static final string tag_message = "message"; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.login); //setup input fields user = (edittext)findviewbyid(r.id.edtusername); pass = (edittext)findviewbyid(r.id.edtpassword); //setup buttons msubmit = (button)findviewbyid(r.id.signin); mregister = (button)findviewbyid(r.id.register); //register listeners msubmit.setonclicklistener(this); mregister.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.signin: new attemptlogin().execute(); break; case r.id.register: intent = new intent(this, registrationform.class); startactivity(i); break; default: break; } } class attemptlogin extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ boolean failure = false; @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(login.this); pdialog.setmessage("attempting login..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected string doinbackground(string... args) { // todo auto-generated method stub // check success tag int success; string username = user.gettext().tostring(); string password = pass.gettext().tostring(); log.d(username, username); try { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("username", username)); params.add(new basicnamevaluepair("password", password)); log.d("request!", "starting"); // getting product details making http request jsonobject json = jsonparser.makehttprequest( login_url, "post", params); // check log json response log.d("login attempt", json.tostring()); // json success tag success = json.getint(tag_success); if (success == 1) { log.d("login successful!", json.tostring()); intent = new intent(login.this, mainactivity1.class); finish(); startactivity(i); return json.getstring(tag_message); }else{ log.d("login failure!", json.getstring(tag_message)); return json.getstring(tag_message); } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once product deleted pdialog.dismiss(); if (file_url != null){ toast.maketext(login.this, file_url, toast.length_long).show(); } } } @override // Το παρακάτω το πρόσθεσα εγω protected void ondestroy() { if (pdialog != null) { if (pdialog.isshowing()) { pdialog.dismiss(); pdialog = null; } } super.ondestroy(); } }
login.php
?php //load , connect mysql database stuff require("config.inc.php"); $query_params=null; if (!empty($_post)) { //gets user's info based off of username. $query = " select id, username, password registration username = :username "; $query_params = array( ':username' => $_post['username'] ); try { $stmt = $db->prepare($query); $result = $stmt->execute(); $stmt->execute($query_params); } catch (pdoexception $ex) { // testing, use die , message. //die("failed run query: " . $ex->getmessage()); //or use use 1 product json data: $response["success"] = 0; $response["message"] = "database error1. please try again!"; die(json_encode($response)); } //we initialize false. $validated_info = false; //fetching rows query $row = $stmt->fetch(); $login_ok = false; if ($row) { //if encrypted password, unencrypt here, in our case //compare 2 passwords if ($_post['password'] === $row['password']) { $login_ok = true; } } // if user logged in successfully, send them private members-only page // otherwise, display login failed message , show login form again if ($login_ok) { $response["success"] = 1; $response["message"] = "Συνδεθήκατε επιτυχώς!"; die(json_encode($response)); } else { $response["success"] = 0; $response["message"] = "Λανθασμένα στοιχεία!"; die(json_encode($response)); } } else { ?> <h1>login</h1> <form action="login.php" method="post"> username:<br /> <input type="text" name="username" placeholder="username" /> <br /><br /> password:<br /> <input type="password" name="password" placeholder="password" value="" /> <br /><br /> <input type="submit" value="login" /> </form> <a href="register.php">register</a> <?php } ?>
and log.cat
06-07 14:24:08.245: e/json parser(28772): error parsing data org.json.jsonexception: value failed of type java.lang.string cannot converted jsonobject 06-07 14:24:08.315: w/dalvikvm(28772): threadid=12: thread exiting uncaught exception (group=0x411342a0) 06-07 14:24:09.036: e/androidruntime(28772): fatal exception: asynctask #1 06-07 14:24:09.036: e/androidruntime(28772): java.lang.runtimeexception: error occured while executing doinbackground() 06-07 14:24:09.036: e/androidruntime(28772): @ android.os.asynctask$3.done(asynctask.java:299) 06-07 14:24:09.036: e/androidruntime(28772): @ java.util.concurrent.futuretask$sync.innersetexception(futuretask.java:273) 06-07 14:24:09.036: e/androidruntime(28772): @ java.util.concurrent.futuretask.setexception(futuretask.java:124) 06-07 14:24:09.036: e/androidruntime(28772): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:307) 06-07 14:24:09.036: e/androidruntime(28772): @ java.util.concurrent.futuretask.run(futuretask.java:137) 06-07 14:24:09.036: e/androidruntime(28772): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 06-07 14:24:09.036: e/androidruntime(28772): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 06-07 14:24:09.036: e/androidruntime(28772): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 06-07 14:24:09.036: e/androidruntime(28772): @ java.lang.thread.run(thread.java:856) 06-07 14:24:09.036: e/androidruntime(28772): caused by: java.lang.nullpointerexception 06-07 14:24:09.036: e/androidruntime(28772): @ com.example.tranfer.login$attemptlogin.doinbackground(login.java:127) 06-07 14:24:09.036: e/androidruntime(28772): @ com.example.tranfer.login$attemptlogin.doinbackground(login.java:1) 06-07 14:24:09.036: e/androidruntime(28772): @ android.os.asynctask$2.call(asynctask.java:287) 06-07 14:24:09.036: e/androidruntime(28772): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 06-07 14:24:09.036: e/androidruntime(28772): ... 5 more
take @ logcat. says nullpointerexception on login.java:127
means line 127. should success = json.getint(tag_success);
as tag_success
initialized , final must have json
object, initialized here jsonobject json = jsonparser.makehttprequest(login_url, "post", params);
fails ;)
Comments
Post a Comment