This is project in Android login with php and display data in custom listview.
Processing:
Start Login with app and if password have in database, it open new activity and display data in custom listview.Create Database In phpmyadmin.
USE db_client2;
CREATE TABLE tbl_product(pid INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,name VARCHAR(200) NOT NULL,qty int NOT NULL,price float NOT NULL,image_url text NOT NULL);
Insert Data to tbl_product
INSERT INTO tbl_product(name, qty, price,iamge_url)VALUES('name_valuse', 'number_qty', 'price_of_qty,iamge_of_url');
connection.php
<?php
$servername = "localhost"; //replace it with your database server name
$username = "root"; //replace it with your database username
$password = ""; //replace it with your database password
$dbname = "db_client2";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
$servername = "localhost"; //replace it with your database server name
$username = "root"; //replace it with your database username
$password = ""; //replace it with your database password
$dbname = "db_client2";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
login.php
<?PHP
include_once("connection.php");
if( isset($_POST['txtUsername']) && isset($_POST['txtPassword']) ) {
$username = $_POST['txtUsername'];
$password = $_POST['txtPassword'];
$query = "SELECT username, password FROM tbl_client ".
" WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);
if($result->num_rows > 0){
echo "success";
exit;
} else{
echo "Login Failed <br/>";
exit;
}
}
?>
<html>
<head>
<title>Login | Vichit </title>
</head>
<body>
<h1>Login Example|<a href="https://www.youtube.com/channel/UCkIW6gubyhq_17tUI6Qgjog">Vichit Developer Android</a></h1>
<form action="<?PHP $_PHP_SELF ?>" method="post">
Username <input type="text" name="txtUsername" value="" /><br/>
Password <input type="password" name="txtPassword" value="" /><br/>
<input type="submit" name="btnSubmit" value="Login"/>
</form>
</body>
</html>
include_once("connection.php");
if( isset($_POST['txtUsername']) && isset($_POST['txtPassword']) ) {
$username = $_POST['txtUsername'];
$password = $_POST['txtPassword'];
$query = "SELECT username, password FROM tbl_client ".
" WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);
if($result->num_rows > 0){
echo "success";
exit;
} else{
echo "Login Failed <br/>";
exit;
}
}
?>
<html>
<head>
<title>Login | Vichit </title>
</head>
<body>
<h1>Login Example|<a href="https://www.youtube.com/channel/UCkIW6gubyhq_17tUI6Qgjog">Vichit Developer Android</a></h1>
<form action="<?PHP $_PHP_SELF ?>" method="post">
Username <input type="text" name="txtUsername" value="" /><br/>
Password <input type="password" name="txtPassword" value="" /><br/>
<input type="submit" name="btnSubmit" value="Login"/>
</form>
</body>
</html>
In Android: need to add more Library
-JSONConverter.jar >> http://www.mediafire.com/file/tld8dor71pfcb1m/KGJsoConverter.jar
Go to Project >> app>>libs>>past 3 library in to libs Right click on library and than click add as library
MainActivity
you need write some code in method onlick' button
btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { HashMap postData = new HashMap(); String username = edusername.getText().toString(); String password = edpassword.getText().toString(); postData.put("txtUsername", username); postData.put("txtPassword", password); PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() { @Override public void processFinish(String s) { //Log.d(LOG,s); if (s.contains("success")) { Intent intent = new Intent(MainActivity.this,ListActivity.class); startActivity(intent); Toast.makeText(MainActivity.this, "Login Successfully", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Disconnect + /n Please try again", Toast.LENGTH_SHORT).show(); } } }); task.execute("http://10.0.3.2/client/login"); } });
Explain:
http://localhost/client/login >> if you work in php browser, you must to declare this.
http://10.0.3.2/client/login >> if you work in Genymotion, you must to declare this.
Product Class:
This class, if in java we call it Getter and Setter but in android get data from Json we don't use getter
and setter. We use @SerializedNamepublic class Product { @SerializedName("pid") public int pid;
@SerializedName("name") public String name;
@SerializedName("qty") public int qty;
@SerializedName("price") public float price;
@SerializedName("image_url") public String image_url; }
ListActivity:
declare variable:
final String LOG = "ListActivity"; private ArrayList<Product> productsList; private ListView listProduct;OnCreate method:PostResponseAsyncTask task = new PostResponseAsyncTask(ListActivity.this, this);task.execute("http://10.0.3.2/client/product.php");When you make PostResponseAsyncTask, It generator one method to us.processFinishYou can read my command my note on codeproductsList = new JsonConverter<Product>().toArrayList(s, Product.class);BindDictionary<Product> dist = new BindDictionary<>();//Get Name from Product Classdist.addStringField(R.id.tvName, new StringExtractor<Product>() {@Override public String getStringValue(Product product, int position) { return product.name; } }); //Get Qty from Product Classdist.addStringField(R.id.tvQty, new StringExtractor<Product>() { @Override public String getStringValue(Product product, int position) { return product.qty + ""; } }); //Get Price from Product Classdist.addStringField(R.id.tvPrice, new StringExtractor<Product>() { @Override public String getStringValue(Product product, int position) { return product.price + "$"; } }); //Get image from Product Classdist.addDynamicImageField(R.id.imageView, new StringExtractor<Product>() { @Override public String getStringValue(Product product, int position) { return product.image_url; } }, new DynamicImageLoader() { @Override public void loadImage(String url, ImageView imageView) { Picasso.with(ListActivity.this).load(url).into(imageView); } }); FunDapter<Product> adapter = new FunDapter<>(ListActivity.this,productsList,R.layout.layout_list,dist); listProduct = (ListView) findViewById(R.id.lvListView); listProduct.setAdapter(adapter);
One of problem that we want to call Image:
Before we call method loadImage, We must to declare: compile 'com.squareup.picasso:picasso:2.5.2'
Go to bulid.gradle and write
Atfer finish declare. It we show you one problem in Android Monitor. Show this problem we can fix it.
Go to AndroidManifest and add some tools:
xmlns:tools="http://schemas.android.com/tools"tools:replace="@android:icon"

0 Komentar