2023年9月19日 星期二

Android Studio - Android Java Login PHP 登入程式(一百一十二)

Android Studio - Android Java Login PHP 登入程式(一百一十二):

首先是編寫 Android Login 程式,Android 程式負責登入功能,將輸入使用者的帳號、密碼傳送給後端 PHP 處理,並回傳登入是否成功的訊息。

Android Java Login PHP 登入程式
  • 操作系統:Windows 7 64-bit 版本
  • 開發環境:Android Studio 4.0.1 版本
  • 測試手機:Samsung Galaxy M33 5G
  • 測試手機系統版本:Android 12(Snow Cone / 2021)Android Studio 12 版本(Snow Cone)Build.VERSION_CODES.S / SDK 31
  • 原程式:C:\Development\Development_Android\Android_Project\DIY-Android-107-08b LoginBugworkshop Bugworkshop 20230914
  • 程式:C:\Development\Development_Android\Android_Project\ DIY-Android-107-08b LoginBugworkshop Bugworkshop 20230914

Android Java Login PHP 登入程式編程步驟:
  • 輸入個人電郵和密碼,按 Login 鍵
  • 程式會將電郵和密碼轉成 JSON,然後 POST 到 NAS PHP 的 URL
  • PHP Server 接收電郵和密碼,便會連接 MySQL 並查詢電郵和密碼是否正確,將結果傳回手機
  • 手機接收結果,並顯示解讀

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="bugworkshop.loginSystem">

 

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-permission android:name="android.permission.INTERNET" />

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme"

        android:usesCleartextTraffic="true">

 

        <activity android:name=".Login>

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>


activity_login.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:textAlignment="center">

 

    <TextView

        android:id="@+id/textUser"

        android:layout_width="80dp"

        android:layout_height="57dp"

        android:layout_marginStart="20dp"

        android:layout_marginTop="48dp"

        android:text="User :"

        android:textSize="30sp"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

 

    <EditText

        android:id="@+id/email"

        android:layout_width="234dp"

        android:layout_height="57dp"

        android:layout_marginStart="10dp"

        android:layout_marginTop="48dp"

        android:inputType="text"

        android:textSize="30sp"

        app:layout_constraintStart_toEndOf="@+id/textUser"

        app:layout_constraintTop_toTopOf="parent" />

 

    <TextView

        android:id="@+id/textPassword"

        android:layout_width="150dp"

        android:layout_height="50dp"

        android:layout_marginStart="20dp"

        android:layout_marginTop="24dp"

        android:text="Password :"

        android:textSize="30sp"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/textUser" />

 

    <EditText

        android:id="@+id/password"

        android:layout_width="176dp"

        android:layout_height="51dp"

        android:layout_marginStart="10dp"

        android:layout_marginTop="24dp"

        android:inputType="text"

        android:password="true"

        android:textSize="30sp"

        app:layout_constraintStart_toEndOf="@+id/textPassword"

        app:layout_constraintTop_toBottomOf="@+id/email" />

 

    <Button

        android:id="@+id/signin"

        android:layout_width="206dp"

        android:layout_height="60dp"

        android:layout_marginTop="32dp"

        android:text="LOGIN"

        android:textSize="30sp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.497"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/password" />

 

    <TextView

        android:id="@+id/hello"

        android:layout_width="299dp"

        android:layout_height="53dp"

        android:layout_marginTop="36dp"

        android:text="Hello"

        android:textAlignment="center"

        android:textSize="24sp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.504"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/signin" />

 

</androidx.constraintlayout.widget.ConstraintLayout>


Login.java:

package bugworkshop.loginSystem;

 

import androidx.appcompat.app.AppCompatActivity;

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 android.widget.Toast;

 

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 org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

 

import java.util.HashMap;

import java.util.Map;

 

public class Login extends AppCompatActivity {

 

    EditText password, email;

    TextView hello;

    Button signin;

    TextView alert;

 

    String pass,mail;

    public RequestQueue mQueue;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

 

        password = findViewById(R.id.password);

        email = findViewById(R.id.email);

        hello = findViewById(R.id.hello);

        signin = findViewById(R.id.signin);

 

        signin.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                checklogin();

            }

        });

    }

 

    private void checklogin() {

 

        mail = email.getText().toString().trim();

        pass  = password.getText().toString().trim();

 

        String url = "http://192.168.0.122/~test/efarm//login.php";

 

        StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {

            @Override

            public void onResponse(String response) {

                try {

                    JSONObject jsonobject = new JSONObject(response);

                    String success = jsonobject.getString("success");

                    JSONArray jsonarray = jsonobject.getJSONArray("login");

                    JSONObject jo = jsonarray.getJSONObject(0);

 

                    if(success.equals("1")) {

                        hello.setText(jo.getString("Name") + " Login OK");

                    }

                    else{

                        hello.setText("Wrong Username or Password");

                    }

                } catch (JSONException e) {

                    e.printStackTrace();

                }

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError error) {

                error.printStackTrace();

                hello.setText("Error Logging in check Internet Connection");

                Toast.makeText(getApplicationContext(), "Error Logging in check Internet Connection", Toast.LENGTH_LONG).show();

            }

        }){

            @Override

            protected Map<String,String> getParams(){

                HashMap<String,String> param = new HashMap<String,String>();

                param.put("email",mail);

                param.put("password",pass);

                return param;

            }

 

            @Override

            public Map<String, String> getHeaders() throws AuthFailureError {

                Map<String,String> params = new HashMap<String, String>();

                params.put("Content-Type","application/x-www-form-urlencoded");

                return params;

            }

        };

 

        mQueue = Volley.newRequestQueue(Login.this);

        mQueue.add(sr);

    }

}


2023年 9月 19日(Tue)天氣報告
氣溫:54.0°F / 12.0°C @ 07:00
風速:每小時 2公里
降雨機會:2%
相對濕度:百分之 68%
天氣:多雲時陰

沒有留言:

張貼留言