sql - Android registration and login using MySQL -
i'm working on android app, , stuck function of user registration , log-in.
i need store info on mysql db.
now have code function not running. have errors not figure out.
here main java file:
package com.example.androidhive; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import com.example.androidhive.library.userfunctions; public class dashboardactivity extends activity { userfunctions userfunctions; button btnlogout; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /** * dashboard screen application * */ // check login status in database userfunctions = new userfunctions(); if(userfunctions.isuserloggedin(getapplicationcontext())){ setcontentview(r.layout.dashboard); btnlogout = (button) findviewbyid(r.id.btnlogout); btnlogout.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub userfunctions.logoutuser(getapplicationcontext()); intent login = new intent(getapplicationcontext(), loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); // closing dashboard screen finish(); } }); }else{ // user not logged in show login screen intent login = new intent(getapplicationcontext(), loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); // closing dashboard screen finish(); } }}
my registration function
package com.example.androidhive; import org.json.jsonexception; import org.json.jsonobject; import com.example.androidhive.library.databasehandler; import com.example.androidhive.library.userfunctions; import android.app.activity; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class registeractivity extends activity { button btnregister; button btnlinktologin; edittext inputfullname; edittext inputemail; edittext inputpassword; textview registererrormsg; // json response node names private static string key_success = "success"; private static string key_error = "error"; private static string key_error_msg = "error_msg"; private static string key_uid = "uid"; private static string key_name = "name"; private static string key_email = "email"; private static string key_created_at = "created_at"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.register); // importing assets buttons, text fields inputfullname = (edittext) findviewbyid(r.id.registername); inputemail = (edittext) findviewbyid(r.id.registeremail); inputpassword = (edittext) findviewbyid(r.id.registerpassword); btnregister = (button) findviewbyid(r.id.btnregister); btnlinktologin = (button) findviewbyid(r.id.btnlinktologinscreen); registererrormsg = (textview) findviewbyid(r.id.register_error); // register button click event btnregister.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { string name = inputfullname.gettext().tostring(); string email = inputemail.gettext().tostring(); string password = inputpassword.gettext().tostring(); userfunctions userfunction = new userfunctions(); jsonobject json = userfunction.registeruser(name, email, password); // check login response seek { if (json.getstring(key_success) != null) { registererrormsg.settext(""); string res = json.getstring(key_success); if(integer.parseint(res) == 1){ // user registred // store user details in sqlite database databasehandler db = new databasehandler(getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); // clear previous info in database userfunction.logoutuser(getapplicationcontext()); db.adduser(json_user.getstring(key_name), json_user.getstring(key_email), json.getstring(key_uid), json_user.getstring(key_created_at)); // launch dashboard screen intent dashboard = new intent(getapplicationcontext(), dashboardactivity.class); // close views before launching dashboard dashboard.addflags(intent.flag_activity_clear_top); startactivity(dashboard); // close registration screen finish(); }else{ // error in registration registererrormsg.settext("error occured in registration"); } } } grab (jsonexception e) { e.printstacktrace(); } } }); // link login screen btnlinktologin.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent = new intent(getapplicationcontext(), loginactivity.class); startactivity(i); // close registration view finish(); } }); }}
my log-in
package com.example.androidhive; import java.util.hashmap; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import com.example.androidhive.library.databasehandler; import com.example.androidhive.library.userfunctions; public class loginactivity extends activity { button btnlogin; button btnlinktoregister; edittext inputemail; edittext inputpassword; textview loginerrormsg; // json response node names private static string key_success = "success"; private static string key_error = "error"; private static string key_error_msg = "error_msg"; private static string key_uid = "uid"; private static string key_name = "name"; private static string key_email = "email"; private static string key_created_at = "created_at"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.login); // importing assets buttons, text fields inputemail = (edittext) findviewbyid(r.id.loginemail); inputpassword = (edittext) findviewbyid(r.id.loginpassword); btnlogin = (button) findviewbyid(r.id.btnlogin); btnlinktoregister = (button) findviewbyid(r.id.btnlinktoregisterscreen); loginerrormsg = (textview) findviewbyid(r.id.login_error); // login button click event btnlogin.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { string email = inputemail.gettext().tostring(); string password = inputpassword.gettext().tostring(); userfunctions userfunction = new userfunctions(); log.d("button", "login"); jsonobject json = userfunction.loginuser(email, password); // check login response seek { if (json.getstring(key_success) != null) { loginerrormsg.settext(""); string res = json.getstring(key_success); if(integer.parseint(res) == 1){ // user logged in // store user details in sqlite database databasehandler db = new databasehandler(getapplicationcontext()); jsonobject json_user = json.getjsonobject("user"); // clear previous info in database userfunction.logoutuser(getapplicationcontext()); db.adduser(json_user.getstring(key_name), json_user.getstring(key_email), json.getstring(key_uid), json_user.getstring(key_created_at)); // launch dashboard screen intent dashboard = new intent(getapplicationcontext(), dashboardactivity.class); // close views before launching dashboard dashboard.addflags(intent.flag_activity_clear_top); startactivity(dashboard); // close login screen finish(); }else{ // error in login loginerrormsg.settext("incorrect username/password"); } } } grab (jsonexception e) { e.printstacktrace(); } } }); // link register screen btnlinktoregister.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent = new intent(getapplicationcontext(), registeractivity.class); startactivity(i); finish(); } }); }}
my database handler
package com.example.androidhive.library; import java.util.hashmap; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasehandler extends sqliteopenhelper { // static variables // database version private static final int database_version = 1; // database name private static final string database_name = "android_api"; // login table name private static final string table_login = "login"; // login table columns names private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_email = "email"; private static final string key_uid = "uid"; private static final string key_created_at = "created_at"; public databasehandler(context context) { super(context, database_name, null, database_version); } // creating tables @override public void oncreate(sqlitedatabase db) { string create_login_table = "create table " + table_login + "(" + key_id + " integer primary key," + key_name + " text," + key_email + " text unique," + key_uid + " text," + key_created_at + " text" + ")"; db.execsql(create_login_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table_login); // create tables 1 time again oncreate(db); } /** * storing user details in database * */ public void adduser(string name, string email, string uid, string created_at) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, name); // name values.put(key_email, email); // email values.put(key_uid, uid); // email values.put(key_created_at, created_at); // created @ // inserting row db.insert(table_login, null, values); db.close(); // closing database connection } /** * getting user info database * */ public hashmap<string, string> getuserdetails(){ hashmap<string,string> user = new hashmap<string,string>(); string selectquery = "select * " + table_login; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(selectquery, null); // move first row cursor.movetofirst(); if(cursor.getcount() > 0){ user.put("name", cursor.getstring(1)); user.put("email", cursor.getstring(2)); user.put("uid", cursor.getstring(3)); user.put("created_at", cursor.getstring(4)); } cursor.close(); db.close(); // homecoming user homecoming user; } /** * getting user login status * homecoming true if rows there in table * */ public int getrowcount() { string countquery = "select * " + table_login; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); int rowcount = cursor.getcount(); db.close(); cursor.close(); // homecoming row count homecoming rowcount; } /** * re crate database * delete tables , create them 1 time again * */ public void resettables(){ sqlitedatabase db = this.getwritabledatabase(); // delete rows db.delete(table_login, null, null); db.close(); }}
my json parser
package com.example.androidhive.library; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.list; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import android.util.log; public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } public jsonobject getjsonfromurl(string url, list<namevaluepair> params) { // making http request seek { // 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(); } grab (unsupportedencodingexception e) { e.printstacktrace(); } grab (clientprotocolexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } seek { 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(); log.e("json", json); } grab (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // seek parse string json object seek { jobj = new jsonobject(json); } grab (jsonexception e) { log.e("json parser", "error parsing info " + e.tostring()); } // homecoming json string homecoming jobj; }}
userfunctions
package com.example.androidhive.library; import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonobject; import android.content.context; public class userfunctions { private jsonparser jsonparser; private static string loginurl = "http://10.0.2.2/ah_login_api/"; private static string registerurl = "http://10.0.2.2/ah_login_api/"; private static string login_tag = "login"; private static string register_tag = "register"; // constructor public userfunctions(){ jsonparser = new jsonparser(); } /** * function create login request * @param email * @param password * */ public jsonobject loginuser(string email, string password){ // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("tag", login_tag)); params.add(new basicnamevaluepair("email", email)); params.add(new basicnamevaluepair("password", password)); jsonobject json = jsonparser.getjsonfromurl(loginurl, params); // homecoming json // log.e("json", json.tostring()); homecoming json; } /** * function create login request * @param name * @param email * @param password * */ public jsonobject registeruser(string name, string email, string password){ // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("tag", register_tag)); params.add(new basicnamevaluepair("name", name)); params.add(new basicnamevaluepair("email", email)); params.add(new basicnamevaluepair("password", password)); // getting json object jsonobject json = jsonparser.getjsonfromurl(registerurl, params); // homecoming json homecoming json; } /** * function login status * */ public boolean isuserloggedin(context context){ databasehandler db = new databasehandler(context); int count = db.getrowcount(); if(count > 0){ // user logged in homecoming true; } homecoming false; } /** * function logout user * reset database * */ public boolean logoutuser(context context){ databasehandler db = new databasehandler(context); db.resettables(); homecoming true; }}
and here comes erro logcat:
02-12 14:11:09.267: e/json(388): <?xml version="1.0" encoding="utf-8"?> 02-12 14:11:09.267: e/json(388): <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" 02-12 14:11:09.267: e/json(388): "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> 02-12 14:11:09.267: e/json(388): <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 02-12 14:11:09.267: e/json(388): <head> 02-12 14:11:09.267: e/json(388): <title>object not found!</title> 02-12 14:11:09.267: e/json(388): <link rev="made" href="mailto:postmaster@localhost" /> 02-12 14:11:09.267: e/json(388): <style type="text/css"><!--/*--><![cdata[/*><!--*/ 02-12 14:11:09.267: e/json(388): body { color: #000000; background-color: #ffffff; } 02-12 14:11:09.267: e/json(388): a:link { color: #0000cc; } 02-12 14:11:09.267: e/json(388): p, address {margin-left: 3em;} 02-12 14:11:09.267: e/json(388): span {font-size: smaller;} 02-12 14:11:09.267: e/json(388): /*]]>*/--></style> 02-12 14:11:09.267: e/json(388): </head> 02-12 14:11:09.267: e/json(388): <body> 02-12 14:11:09.267: e/json(388): <h1>object not found!</h1> 02-12 14:11:09.267: e/json(388): <p> 02-12 14:11:09.267: e/json(388): requested url not found on server. 02-12 14:11:09.267: e/json(388): 02-12 14:11:09.267: e/json(388): if entered url manually please check 02-12 14:11:09.267: e/json(388): spelling , seek again. 02-12 14:11:09.267: e/json(388): 02-12 14:11:09.267: e/json(388): </p> 02-12 14:11:09.267: e/json(388): <p> 02-12 14:11:09.267: e/json(388): if think server error, please contact 02-12 14:11:09.267: e/json(388): <a href="mailto:postmaster@localhost">webmaster</a>. 02-12 14:11:09.267: e/json(388): </p> 02-12 14:11:09.267: e/json(388): <h2>error 404</h2> 02-12 14:11:09.267: e/json(388): <address> 02-12 14:11:09.267: e/json(388): <a href="/">10.0.2.2</a><br /> 02-12 14:11:09.267: e/json(388): <span>apache/2.4.3 (win32) openssl/1.0.1c php/5.4.7</span> 02-12 14:11:09.267: e/json(388): </address> 02-12 14:11:09.267: e/json(388): </body> 02-12 14:11:09.267: e/json(388): </html> 02-12 14:11:09.408: e/json parser(388): error parsing info org.json.jsonexception: value <?xml of type java.lang.string cannot converted jsonobject 02-12 14:11:09.477: e/androidruntime(388): fatal exception: main 02-12 14:11:09.477: e/androidruntime(388): java.lang.nullpointerexception 02-12 14:11:09.477: e/androidruntime(388): @ com.example.androidhive.registeractivity$1.onclick(registeractivity.java:64) 02-12 14:11:09.477: e/androidruntime(388): @ android.view.view.performclick(view.java:2408) 02-12 14:11:09.477: e/androidruntime(388): @ android.view.view$performclick.run(view.java:8816) 02-12 14:11:09.477: e/androidruntime(388): @ android.os.handler.handlecallback(handler.java:587) 02-12 14:11:09.477: e/androidruntime(388): @ android.os.handler.dispatchmessage(handler.java:92) 02-12 14:11:09.477: e/androidruntime(388): @ android.os.looper.loop(looper.java:123) 02-12 14:11:09.477: e/androidruntime(388): @ android.app.activitythread.main(activitythread.java:4627) 02-12 14:11:09.477: e/androidruntime(388): @ java.lang.reflect.method.invokenative(native method) 02-12 14:11:09.477: e/androidruntime(388): @ java.lang.reflect.method.invoke(method.java:521) 02-12 14:11:09.477: e/androidruntime(388): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 02-12 14:11:09.477: e/androidruntime(388): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 02-12 14:11:09.477: e/androidruntime(388): @ dalvik.system.nativestart.main(nativemethod)
i need help final year project , stuck connection months :o
so cooperation much appreciated anyway guys please best guide me
after analyzing logcat, there appears 2 problems:
the requested url not found, http 404 you trying convert received response http server json object. in case since url not found server has returned xml construction , hence can't converted json object.these first problems you'd need address.
android sql xml json sqlite
No comments:
Post a Comment