Android login form the values does not pass to the database -
Android login form the values does not pass to the database -
i created login form android application
here's mainactivity.java file
bundle com.chi.tripassist; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; 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.apache.http.params.basichttpparams; import org.apache.http.params.httpparams; import org.json.jsonobject; import android.support.v7.app.actionbaractivity; import android.content.sharedpreferences; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class mainactivity extends actionbaractivity implements onclicklistener { edittext username, password; button btnsub; string username, password; httpclient httpclient; httppost httppost; arraylist<namevaluepair>namevaluepairs; httpresponse response; httpentity entity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); username=(edittext) findviewbyid(r.id.username); password=(edittext) findviewbyid(r.id.password); btnsub=(button) findviewbyid(r.id.btnsub); btnsub.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } @override public void onclick(view v) { httpparams httpparameter = new basichttpparams(); httpclient=new defaulthttpclient(httpparameter); httppost=new httppost("http://192.168.43.2/mydb/connection.php"); username=username.gettext().tostring(); password=password.gettext().tostring(); seek { namevaluepairs=new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("username",username)); namevaluepairs.add(new basicnamevaluepair("password",password)); httppost.setentity(new urlencodedformentity(namevaluepairs)); response=httpclient.execute(httppost); if(response.getstatusline().getstatuscode()==200) { entity=response.getentity(); if(entity != null) { inputstream instream=entity.getcontent(); jsonobject jsonresponse=new jsonobject(convertstreamtostring(instream)); string username=jsonresponse.getstring("username"); string password=jsonresponse.getstring("password"); if(username.equals(username) && password.equals(password)) { sharedpreferences sp=getsharedpreferences("logindetails", 0); sharedpreferences.editor spedit=sp.edit(); spedit.putstring("username", username); spedit.putstring("password", password); spedit.commit(); toast.maketext(getbasecontext(), "success!", toast.length_short).show(); } else { toast.maketext(getbasecontext(), "invalid username & password", toast.length_short).show(); } } } }catch(exception e) { e.printstacktrace(); toast.maketext(getbasecontext(), "connection error", toast.length_short).show(); } // todo auto-generated method stub } private static string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; seek { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } grab (ioexception e) { e.printstacktrace(); } { seek { is.close(); } grab (ioexception e) { e.printstacktrace(); } } homecoming sb.tostring(); } }
here's activity_main.xml file
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.chi.tripassist.mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="@string/title" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hntuser" android:id="@+id/username" android:inputtype="text"/> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hntpwd" android:id="@+id/password" android:inputtype="textpassword"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnsub" android:text="@string/login"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnreg" android:text="@string/signup"/> </linearlayout>
and here's manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.chi.tripassist" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="21" /> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
connection.php file
<?php $dbhost="localhost"; $dbuser="root"; $dbpwd=""; $db="mydb"; $con=mysql_connect($dbhost,$dbuser,$dbpwd,$db)or die("connnection error"); mysql_select_db($db)or die("database selection error"); $username=$_post['username']; $password=$_post['password']; $sql=mysql_query("select * users username='$username' , password='$password'"); $num=mysql_num_rows($sql); if($num==1) { while($list=mysql_fetch_assoc($sql)) { $output=$list; echo json_encode($output); mysql_close(); } } ?> ran app on phone , gave ip address on wifi hotspot
it gives toast "connection error!" in grab part of seek grab block.
the username , password 2 fields in users table.
i created little html page , test php file , gave jason output. cant login.
android database connection
Comments
Post a Comment