Contoh kode login android dan php dengan volley

Berikut ini adalah contoh untuk membuat login di android ke php dengan menggunakan volley library, silahkan di pelajari dan di kembangkan sendiri.

File MainActivity.java

package com.example.testlogin;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	Button button;
	EditText username, password;
	
	String server_url = "http://192.168.1.100/api/login.php";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button = (Button) findViewById(R.id.b_id_ok);
        username = (EditText) findViewById(R.id.et_id_username);
        password = (EditText) findViewById(R.id.et_id_password);
        
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
                // TODO Auto-generated method stub
				final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
				final String Username, Password;
				
				Username = username.getText().toString();
				Password = password.getText().toString();
				
				StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url, 
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						if(response.equals("1")) {
							Toast.makeText(MainActivity.this, "Login success ...", Toast.LENGTH_LONG).show();
						} else {
							Toast.makeText(MainActivity.this, response + "Invalid username or password ...", Toast.LENGTH_LONG).show();							
						}						
						requestQueue.stop();						
					}		
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Toast.makeText(MainActivity.this, "Error ...", Toast.LENGTH_LONG).show();
						error.printStackTrace();
						requestQueue.stop();
					}
				}){
					@Override
					protected Map<String, String> getParams() throws AuthFailureError{
						Map<String, String> params = new HashMap<String, String>();
						params.put("username", Username);
						params.put("password", Password);
						return params;
					}

				};
				requestQueue.add(stringRequest);
			}
		});
	}

}

 

File activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top">

    <TextView
        android:id="@+id/tv_id_login"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/tv_login"
        android:textSize="20sp" />
    
    <TextView
    	android:id="@+id/tv_id_username"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="@string/tv_username" 
	    android:layout_below="@id/tv_id_login"
    />

    <EditText
	    android:id="@+id/et_id_username"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:inputType="text"
	    android:layout_below="@id/tv_id_username"
    />

    <TextView
	    android:id="@+id/tv_id_password"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="@string/tv_password"
	    android:layout_below="@id/et_id_username"
    />

    <EditText
	    android:id="@+id/et_id_password"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:inputType="textPassword"
	    android:layout_below="@id/tv_id_password"
    />

    <Button
	    android:id="@+id/b_id_ok"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/et_id_password"
	    android:text="@string/b_ok" 
    />


</RelativeLayout>

 

File login.php

<?php
// database
include_once("../db.php");

if($_POST) {

	$result = mysql_query("select * " . 
		" from user " .
		" and username = '" . $_POST['username'] . "' " .
		" and password = '" . md5($_POST['password']) . "' " );
        
	if(mysql_num_rows($result)>0) {
		while($row = mysql_fetch_array($result)) {
            echo "1";
		}
	} else {
        echo "";
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s