php - Android login validation with external db doesn't work -
i'm implementing android app can track whereabouts of family members... , first try of developing android app. created databse using wamp phpmyadmin. when run project , provide username (chatfun) & password (chatpass) , hit "login" doesn't & ft_login.php keep displaying "please enter username & password"...when click "sign group" i'm redirected next layout properly...please me find went wrong.. thank in advance..
login table
uid | username | password | fname | mname | location |
1 | chatfun | chatpass | jen | lee | null |
2 | lazlomax | lazlopass | lazlo | joe | null |
ft_login.php
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>ftlogin</title> </head> <?php $dbhost="localhost"; $dbuser="root"; $dbpass="test123"; $dbdb="familytracker"; $connect = mysqli_connect($dbhost,$dbuser,$dbpass); if (mysqli_connect_errno($connect)) { echo "failed connect mysql".mysqli_connect_errno(); } $db = mysqli_select_db($connect,$dbdb); if (!$db) { echo "failed connect database".mysqli_error(); } else { if(isset($_post["username2"],$_post["password2"] )) { $username2 = $_post["username2"]; print_r($username2); $password2 = $_post["password2"]; print_r($password2); $query = mysqli_query("select username,password login username='".$username2."' , password='".$password2."' "); print_r($query); $num = mysqli_num_rows($query); print_r($num); if($num==1) { while($list = mysqli_fetch_assoc($query)) { $output = $list; } echo json_encode($output); } else { echo "invalid username/password! "; } mysqli_close(); } else { echo "please enter username & password"; } } ?> <body> </body> </html> login.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/layoutbg" > <tablelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:layout_weight="0.45" > <tablerow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </tablerow> <tablerow android:id="@+id/tablerow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp" > <imageview android:id="@+id/imageview1" android:layout_width="match_parent" android:layout_height="150dp" android:layout_gravity="center" android:layout_weight="1" android:src="@drawable/login_pic" /> </tablerow> <tablerow android:id="@+id/tablerow3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="15dp" android:layout_margintop="15dp" > <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <textview android:id="@+id/tvuserid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginright="20dp" android:text="username" android:textappearance="?android:attr/textappearancemedium" android:textsize="16dp" /> <edittext android:id="@+id/etusername" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/gradiant" android:ems="10" > <requestfocus /> </edittext> </linearlayout> </tablerow> <tablerow android:id="@+id/tablerow4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="15dp" android:layout_margintop="15dp" > <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <textview android:id="@+id/tvpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" android:textappearance="?android:attr/textappearancemedium" android:textsize="16dp" /> <edittext android:id="@+id/etpass" android:layout_width="220dp" android:layout_height="wrap_content" android:layout_marginleft="19dp" android:layout_weight="0.42" android:background="@drawable/gradiant" android:ems="10" android:inputtype="textpassword" /> </linearlayout> </tablerow> <tablerow android:id="@+id/tablerow5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="15dp" > <button android:id="@+id/btnlogin" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableleft="@drawable/padlock" android:text="login" android:textsize="18dp" /> </tablerow> <tablerow android:id="@+id/tablerow6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="15dp" > <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <imageview android:id="@+id/imageview2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginleft="145dp" android:layout_weight="0.04" android:src="@drawable/exclamation" /> <button android:id="@+id/btnsignup" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="35dp" android:layout_weight="0.33" android:background="@color/layoutbg" android:text="sign group" android:textsize="16dp" /> </linearlayout> </tablerow> </tablelayout> </linearlayout> loginactivity.java
package com.awd.gps; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.uri; import java.net.urisyntaxexception; import java.util.arraylist; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.json.jsonobject; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.media.mediaplayer; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class loginactivity extends activity{ string username1,password1,fname1,mname1; edittext uid,pass; button loginbutton; httpclient httpclient; httppost httppost; httpentity httpentity; httpresponse httpresponse; arraylist<namevaluepair> namevaluepairs; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.login); initialize(); } private void initialize() { // todo auto-generated method stub final mediaplayer mpbuttonclick = mediaplayer.create(getapplicationcontext(), r.raw.buttonclick); uid = (edittext) findviewbyid(r.id.etusername); pass = (edittext) findviewbyid(r.id.etpass); loginbutton = (button) findviewbyid(r.id.btnlogin); loginbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub final mediaplayer mpbuttonclick = mediaplayer.create(getapplicationcontext(), r.raw.buttonclick); httpclient = new defaulthttpclient(); httppost = new httppost("http://localhost:8080/ft_login.php"); username1 = uid.gettext().tostring(); password1 = pass.gettext().tostring(); try { namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("username2",username1.trim())); namevaluepairs.add(new basicnamevaluepair("password2",password1.trim())); namevaluepairs.add(new basicnamevaluepair("fname2", fname1.trim())); namevaluepairs.add(new basicnamevaluepair("mname2", mname1.trim())); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse = httpclient.execute(httppost); if(httpresponse.getstatusline().getstatuscode()==200) { httpentity = httpresponse.getentity(); if(httpentity !=null) { inputstream = httpentity.getcontent(); jsonobject jsonresponse = new jsonobject(convertstreamtostring(is)); string locallid = jsonresponse.getstring("uid");//mysql table field string localuser = jsonresponse.getstring("username");//mysql table field string localpass = jsonresponse.getstring("password");//mysql table field string localfname = jsonresponse.getstring("fname");//mysql table field string locallname = jsonresponse.getstring("mname");//mysql table field string locallocation = jsonresponse.getstring("location");//mysql table field if(uid.gettext().tostring().equals(localuser) && pass.gettext().tostring().equals(localpass)) { sharedpreferences sp = getsharedpreferences("logindetails",0); sharedpreferences.editor spedit = sp.edit(); spedit.putstring("username",username1); spedit.putstring("password",password1); spedit.putstring("fname", fname1); spedit.putstring("mname", mname1); spedit.commit(); mpbuttonclick.start(); toast.maketext(getapplicationcontext(),"welcome family tracker",toast.length_short).show(); startactivity(new intent("com.awd.gps.map")); } else if(username1.equals(localuser) ||password1.equals(localpass)) { mpbuttonclick.start(); toast.maketext(getapplicationcontext(),"invalid username or password please try again",toast.length_short).show(); } else { mpbuttonclick.start(); toast.maketext(getapplicationcontext(),"please sign 'family tracker' first ",toast.length_short).show(); startactivity(new intent("com.awd.gps.signup")); } } } } catch(exception e) { e.printstacktrace(); } } }); button signupbutton = (button) findviewbyid(r.id.btnsignup); signupbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub startactivity(new intent("com.awd.gps.signup")); mpbuttonclick.start(); } }); } private static string convertstreamtostring(inputstream is) { // todo auto-generated method stub bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); } }
Comments
Post a Comment