|  | 
	                
 
 
  
    | 
        
        Android : ระบบ login หลังจาก Check user&pass จาก MySQL แล้ว ถ้าถูกจะให้ไปหน้าต่อไปต้องทำไงหรอครับ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | คือตอนนี้ผมทำระบบ login ครับ แล้วทีนี้ อยากให้เช็คค่า username และ password คือหลังจากเช็คแล้วถูกต้อง อยากให้ลิ้งไปหน้าต่อไปทำไงหรอครับ อันนี้ไม่ทราบจริงๆครับ
 
 อีกข้อคือ ผมไม่ทราบว่าใช้  JSON ถูกหรือเปล่าครับทำไมข้อความมันออกมาแบบมั่วๆรบกวนช่วยดูให้ทีครับ
 
 login.php
 
 <?php
require "conn.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from employee_data where username like '$user_name' and password like '$user_pass';";
$result = mysqli_query($conn ,$mysql_qry);
if(mysqli_num_rows($result) > 0){
echo "Login Success";
}
else
{
	echo"Login not success";}
?>
 
 register.php
 
 <?php
require "conn.php";
$name = $_POST["name"];
$surname = $_POST["surname"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$mysql_qry = "insert into employee_data(name, surname, age, username, password) value ('$name','$surname','$age','$username','$password')";
if($conn->query($mysql_qry) === TRUE){
echo "Insert Success";
}
else
{
	echo"Insert not success".$mysql_qry."<br>". $conn->error;
	}
	$conn->close();
?>
 
 json-get-data.php
 
 <?php
require "conn.php";
 $db_name = "employee101";  
 $mysql_username = "root";  
 $mysql_password = "";  
 $server_name = "localhost"; 
$mysql_qry = "select * from employee_data";
$conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name); 
$result = mysqli_query($conn,$mysql_qry);
$response = array();
while($row = mysqli_fetch_array($result))
{
	array_push($response,array("name"=>$row[1],"surname"=>$row[2],"age"=>$row[3],));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($conn);
?>
 
 MainActivity.java
 
 package com.example.puen.mysqldemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    EditText UsernameEt ,PasswordEt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UsernameEt = (EditText)findViewById(R.id.etUserName);
        PasswordEt = (EditText)findViewById(R.id.etPassword);
    }
    public void OnLogin(View view)
    {
        String username = UsernameEt.getText().toString();
        String password = PasswordEt.getText().toString();
        String type = "login";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username, password);
    }
    public  void  OpenReg(View view){
        startActivity(new Intent(this,Register.class));
    }
    public  void  getJSONClick(View view){
        startActivity(new Intent(this, getDatabase.class));
    }
}
 
 
 Backgroundwrok.java
 
 package com.example.puen.mysqldemo;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
 * Created by puen on 4/6/2016.
 */
public class BackgroundWorker extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx){
        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://192.168.10.125/login.php";
        String register_url = "http://192.168.10.125/register.php";
        if(type.equals("login")){
            try {
                String user_name = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while ((line = bufferedReader.readLine())!= null){
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (type.equals("register")){
            try {
                String name = params[1];
                String surname = params[2];
                String age = params[3];
                String username = params[4];
                String password = params[5];
                URL url = new URL(register_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
                +URLEncoder.encode("surname","UTF-8")+"="+URLEncoder.encode(surname,"UTF-8")+"&"
                +URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(age,"UTF-8")+"&"
                +URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while ((line = bufferedReader.readLine())!= null){
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }
    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
 
 
 getData.java
 
 package com.example.puen.mysqldemo;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class getDatabase extends AppCompatActivity {
    String JSON_STRING;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_database);
    }
    public void getJSON(View view)
    {
        new BackgroundTask().execute();
    }
    class BackgroundTask extends AsyncTask<Void,Void,String> {
        String json_url;
        @Override
        protected void onPreExecute() {
            json_url = "http://192.168.10.125/json_get_data.php";
        }
        @Override
        protected String doInBackground(Void... params) {
            try {
                URL url = new URL(json_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                InputStream inputStream =httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                while ((JSON_STRING = bufferedReader.readLine())!=null)
                {
                    stringBuilder.append(JSON_STRING+"\n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
        @Override
        protected void onPostExecute(String result) {
            TextView textView = (TextView)findViewById(R.id.tv3);
            textView.setText(result);
        }
    }
}
 
 
 
 register.java
 
 package com.example.puen.mysqldemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Register extends AppCompatActivity {
EditText name, surname, age, username, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        name = (EditText)findViewById(R.id.et_name);
        surname = (EditText)findViewById(R.id.et_surname);
        age = (EditText)findViewById(R.id.et_age);
        username = (EditText)findViewById(R.id.et_username);
        password = (EditText)findViewById(R.id.et_password);
    }
    public void OnReg(View view){
        String str_name = name.getText().toString();
        String str_surname = surname.getText().toString();
        String str_age = age.getText().toString();
        String str_username = username.getText().toString();
        String str_password = password.getText().toString();
        String type = "register";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, str_name, str_surname, str_age, str_username, str_password);
        }
        }
 
 activity_main.xml
 
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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.example.puen.mysqldemo.MainActivity">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:hint="Username"
        android:id="@+id/etUserName"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/btnLogin"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="OnLogin"
        />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="Password"
        android:ems="10"
        android:id="@+id/etPassword"
        android:layout_below="@+id/etUserName"
        android:layout_alignLeft="@+id/etUserName"
        android:layout_alignStart="@+id/etUserName" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/btn_reg"
        android:onClick="OpenReg"
        android:layout_below="@+id/btnLogin"
        android:layout_alignLeft="@+id/btnLogin"
        android:layout_alignStart="@+id/btnLogin"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Read Database"
        android:id="@+id/btn_read"
        android:gravity="center"
        android:layout_below="@+id/btn_reg"
        android:onClick="getJSONClick"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
 
 activity_register.xml
 
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Name"
        android:ems="10"
        android:id="@+id/et_name"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Surname"
        android:ems="10"
        android:id="@+id/et_surname"
        android:layout_below="@+id/et_name"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Age"
        android:ems="10"
        android:id="@+id/et_age"
        android:layout_below="@+id/et_surname"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Username"
        android:ems="10"
        android:id="@+id/et_username"
        android:layout_below="@+id/et_age"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:ems="10"
        android:id="@+id/et_password"
        android:layout_below="@+id/et_username"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_reg"
        android:text="Register"
        android:onClick="OnReg"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
 
 
 activity_get_database.xml
 
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Click"
        android:id="@+id/b1"
        android:onClick="getJSON"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:id="@+id/tv3"
        android:layout_marginTop="60dp"
        android:text="SHOW"
        />
</RelativeLayout>
 
 
 Tag : Mobile, MySQL, Android, JAVA
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2016-04-16 14:47:37 | By :
                          rattapongza | View :
                          1837 | Reply :
                          7 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | บทความนี้เลยครับ ใช้ Shared Preferences 
 
  
 Android Shared Preferences : Login Username/Password (PHP/MySQL)
 
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-04-17 21:43:38 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  ลองดูครับ ไม่ยากเลย 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-04-18 11:57:01 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ผมทำไม่ได้อ่ะครับ ไม่ทราบพอจะลองทำให้ดูได้ไหมครับ จนปัญญาจริงๆครับผมยังมือใหม่มากๆ รบกวนทีครับขอร้อง T^T งานโปรเจ็คด้วย คือผมเจอตัวอย่างเขาใส่ใน onPostExecute อ่ะครับ
 
 http://stackoverflow.com/questions/35680813/how-to-call-intent-in-asynctask-or-how-to-start-new-activity-in-onpostexecute
 
 ตามลิ้งนี้ก็ทำไม่ได้ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-04-18 20:20:16 | By :
                            rattapongza |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ในตัวอย่างก็ทำให้ดูแล้วนะครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-04-19 08:09:34 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ต้องลองเรียก URL โดยตรงดูครับ แล้วกำหนดค่าตัวแปร เพื่อที่จะ Test ก่อนครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2017-01-23 09:48:43 | By :
                            mr.win |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |