MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText txt_username, txt_password;
private Button btn_login;
private String uriAPI = "http://study.ar-leung.net/android/index.php";
Intent intent = new Intent();
Bundle bundle = new Bundle();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_username = (EditText) findViewById(R.id.txt_username);
txt_password = (EditText) findViewById(R.id.txt_password);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(sendlistener);
}
public View.OnClickListener sendlistener = new View.OnClickListener(){
public void onClick(View v){
if (v == btn_login){
if (txt_username != null && txt_password != null){
Thread thread = new Thread(new Runnable() { public void run() {
try {
JSONObject obj = sendPostDataToInternet();
System.out.println(obj.getString("username"));
System.out.println(obj.getString("password"));
if(obj.getString("status").equals("success")) {
// Bundle - Store date to other activity
bundle.putString("username", obj.getString("username"));
bundle.putString("password", obj.getString("password"));
intent.putExtras(bundle);
intent.setClass(MainActivity.this, Transaction.class);
startActivity(intent);
MainActivity.this.finish();
}else{
Looper.prepare();
Toast.makeText(MainActivity.this, "Login Fail", Toast.LENGTH_SHORT).show();
Looper.loop();
}
} catch (JSONException e) {
e.printStackTrace();
}
}});
thread.start();
}
}
}
};
private JSONObject sendPostDataToInternet(){
HttpPost httpRequest = new HttpPost(uriAPI);
List params = new ArrayList();
params.add(new BasicNameValuePair("username", txt_username.getText().toString()));
params.add(new BasicNameValuePair("password", txt_password.getText().toString()));
try{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200){
// Get php string
String strResult = EntityUtils.toString(httpResponse.getEntity());
Log.d("JSON", strResult);
// String to JSON Object
JSONObject obj = new JSONObject(strResult);
return obj;
} else{
Log.d("error", "no message");
}
} catch (ClientProtocolException e){
Log.d("@@", e.toString());
} catch (IOException e){
Log.d("@@", e.toString());
} catch (Exception e){
Log.d("@@", e.toString());
}
return null;
}
}
Transaction.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Transaction extends AppCompatActivity {
private Button btn_back;
Intent intent = new Intent();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transaction);
Bundle bundle = getIntent().getExtras();
// Extract the data
String username = bundle.getString("username");
String password = bundle.getString("password");
Log.d("username", username);
Log.d("pasword", password);
btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(sendlistener);
}
public View.OnClickListener sendlistener = new View.OnClickListener(){
public void onClick(View v) {
intent.setClass(Transaction.this, MainActivity.class);
startActivity(intent);
Transaction.this.finish();
}
};
}
AndoridManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.leung.lbms">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Transaction"></activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.example.leung.lbms.MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/label_username"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Username"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/txt_username"
android:layout_width="204dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="leung"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/label_password"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Password"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label_username" />
<EditText
android:id="@+id/txt_password"
android:layout_width="206dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPassword"
android:text="password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label_username" />
<Button
android:id="@+id/btn_login"
android:layout_width="258dp"
android:layout_height="44dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="33dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_password" />
</android.support.constraint.ConstraintLayout>
activity_transaction.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.example.leung.lbms.Transaction">
<Button
android:id="@+id/btn_back"
android:layout_width="155dp"
android:layout_height="wrap_content"
android:layout_marginStart="112dp"
android:layout_marginTop="76dp"
android:text="Back"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="20dp" />
</android.support.constraint.ConstraintLayout>
index.php
<?php
header("Content-type: application/json; charset=utf-8");
if($_POST){
$str = '{"username":"'.$_POST['username'].'","password":"'.$_POST['password'];
if($_POST['username'] == "leung" && $_POST['password'] == "password"){
$str .= '","status":"success"}';
}else{
$str .= '","status":"fail"}';
}
echo $str;
}
?>