Get json and store value to other page in android

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;
	}
?>

201 thoughts on “Get json and store value to other page in android

  1. Pingback: Free viagra

  2. Pingback: generic cialis

  3. Pingback: cialis generic name

  4. Pingback: cialis 20 mg

  5. Pingback: Buy viagra in canada

  6. Pingback: Canada meds viagra

  7. Pingback: cialis coupon walmart

  8. Pingback: cialis price walmart

  9. Pingback: cialis otc

  10. Pingback: cialis 5 mg

  11. Pingback: when will generic cialis be available

  12. Pingback: online pharmacy viagra

  13. Pingback: viagra for sale

  14. Pingback: viagra generic

  15. Pingback: best ed pills at gnc

  16. Pingback: ed medication

  17. Pingback: ed pills for sale

  18. Pingback: cvs pharmacy

  19. Pingback: Get cialis

  20. Pingback: levitra online

  21. Pingback: vardenafil coupon

  22. Pingback: wind creek casino online play

  23. Pingback: real money casino games

  24. Pingback: sildenafil dosage

  25. Pingback: best online casino real money

  26. Pingback: pala casino online

  27. Pingback: generic tadalafil

  28. Pingback: cash loans

  29. Pingback: personal loans

  30. Pingback: quick cash loans

  31. Pingback: viagra cost

  32. Pingback: new cialis

  33. Pingback: buy cialis

  34. Pingback: buy cialis

  35. Pingback: 5 mg cialis

  36. Pingback: cialis buy

  37. Pingback: gambling casino

  38. Pingback: casino slot games

  39. Pingback: best online casino

  40. Pingback: play for real online casino games

  41. Pingback: viagra cost

  42. Pingback: viagra from india

  43. Pingback: cheap viagra online canadian pharmacy

  44. Pingback: cialis online reviews

  45. Pingback: viagra vs cialis

  46. Pingback: online casinos real money

  47. Pingback: free casino games

  48. Pingback: viagra without a doctor prescription

  49. Pingback: buy sildenafil

  50. Pingback: buy viagra generic

  51. Pingback: cialis coupon 2020

  52. Pingback: sildenafil

  53. Pingback: canadian drugs pharmacy

  54. Pingback: best canadian pharmacy

  55. Pingback: viagra achat en france

  56. Pingback: cialis

  57. Pingback: viagra

  58. Pingback: buy cialis online

  59. Pingback: cheap viagra

  60. Pingback: carprofen without vet prescription

  61. Pingback: buy viagra generic

  62. Pingback: Cheap Erection Pills

  63. Pingback: buy tadalafil

  64. Pingback: cialis 20mg pills

  65. Pingback: 5mg cialis

  66. Pingback: au cialis

  67. Pingback: buy amoxicillin online cheap

  68. Pingback: cialis order

  69. Pingback: instant online payday loans

  70. Pingback: Canadian viagra and healthcare

  71. Pingback: where can i buy levitra online

  72. Pingback: Viagra 100 mg

  73. Pingback: law school essay writing service

  74. Pingback: Buy viagra in us

  75. Pingback: leg pain with cialis

  76. Pingback: thesisbyd.com

  77. Pingback: viagra experiences

  78. Pingback: i need help writing an argumentative essay

  79. Pingback: what is a good essay writing service

  80. Pingback: dissertation proposal help

  81. Pingback: essay custom writing

  82. Pingback: professional research paper writers

  83. Pingback: who will write my paper for me

  84. Pingback: write thesis

  85. Pingback: viagra over the counter usa 2020

  86. Pingback: generic cialis order online

  87. Pingback: Pfizer viagra 50mg

  88. Pingback: viagra prices

  89. Pingback: online pharmacy canada

  90. Pingback: buy Amoxil

  91. Pingback: ed doctors

  92. Pingback: order cialis in australia

  93. Pingback: how long does cialis take to work

  94. Pingback: cialis 20 mg

  95. Pingback: cialis pill

  96. Pingback: viagra 100mg

  97. Pingback: buy generic viagra

  98. Pingback: prescription drugs from canada

  99. Pingback: zithromax buy online no prescription

  100. Pingback: cheap generic viagra

  101. Pingback: buy generic viagra

  102. Pingback: over the counter erectile dysfunction pills

  103. Pingback: canadian discount pharmacy

  104. Pingback: my canadian pharmacy

  105. Pingback: Aciclovir for sale

  106. Pingback: canadian pharmacy review

  107. Pingback: shoppers drug mart canada

  108. Pingback: fda approved canadian online pharmacies

  109. Pingback: online pharmacy canada

  110. Pingback: viagra

  111. Pingback: generic viagra online

  112. Pingback: how to get zithromax online

  113. Pingback: how much is amoxicillin

  114. Pingback: 10 mg benadryl tablets

  115. Pingback: periactin 4mg online

  116. Pingback: buy generic 100mg viagra online

  117. Pingback: cheap generic viagra

  118. Pingback: zithromax 500 mg lowest price drugstore online

  119. Pingback: amoxicillin 500 mg cost

  120. Pingback: canadian drugs without prescription

  121. Pingback: sildenafil

  122. Pingback: cialis with dapoxetine (generic)

  123. Pingback: 800 mg cialis

  124. Pingback: cialis coupon

  125. Pingback: where to get the best price on cialis

  126. Pingback: cheap cialis

  127. Pingback: prescription viagra without a doctor

  128. Pingback: kupi online cialis

  129. Pingback: buy cialis united kingdom

  130. Pingback: sildenafil 100mg purchase

  131. Pingback: cheapest generic cialis australia

  132. Pingback: generic viagra

  133. Pingback: generic cialis

  134. Pingback: buy cialis tadalafil tablets

  135. Pingback: natural ed medications

  136. Pingback: gnc ed pills

  137. Pingback: mvuiiess

  138. Pingback: erection pills that work

  139. Pingback: best ed treatment pills

  140. Pingback: what is viagra for

  141. Pingback: buy cialis philippines

  142. Pingback: waar kan ik viagra kopen

  143. Pingback: best place to buy generic viagra online

  144. Pingback: how long does zithromax work in your system

  145. Pingback: furosemida

  146. Pingback: vantin online

  147. Pingback: buy doxycycline generic

  148. Pingback: buy cephalexin online

  149. Pingback: can i buy cialis online

  150. Pingback: buy cialis online

  151. Pingback: is cialis generic available

  152. Pingback: farmacia viagra

  153. Pingback: buy cialis 36 hour

  154. Pingback: uk custom essay writing service

  155. Pingback: free research paper writer

  156. Pingback: i need someone to write my essay

  157. Pingback: help me edit my essay

  158. Pingback: essay on ethics in business

  159. Pingback: how much does cialis cost with insurance

  160. Pingback: buy prescription drugs

  161. Pingback: amoxicillin over the counter usa

  162. Pingback: buy lasix water pill

  163. Pingback: buy viagra online usa

  164. Pingback: where can i order zithromax

  165. Pingback: ivermectin 1 cream 45gm

  166. Pingback: where to buy viagra

  167. Pingback: cheap ventolin uk

  168. Pingback: viagra online usa

  169. Pingback: when will viagra be generic

  170. Pingback: generic zithromax over the counter

  171. Pingback: viagra online usa

  172. Pingback: doxycycline drug class

  173. Pingback: prednisolone medicine

  174. Pingback: clomid increase ejaculate

  175. Pingback: dapoxetine vs caber

  176. Pingback: oral thrush diflucan

  177. Pingback: cost of synthroid

  178. Pingback: essays online to buy

  179. Pingback: natural treatments for ed

  180. Pingback: thesis formatting

  181. Pingback: writing a doctoral dissertation

  182. Pingback: zithromax

  183. Pingback: master thesis writing service

  184. Pingback: neurontin 100mg tab

  185. Pingback: write my essay cheap

  186. Pingback: custom essay company

  187. Pingback: buy zithromax no prescription

  188. Pingback: propecia 5

  189. Pingback: do my papers

  190. Pingback: all generic meds from india

  191. Pingback: neurontin and cymbalta

  192. Pingback: metformin davis

  193. Pingback: order paxil online

  194. Pingback: plaquenil drug manufacturer

  195. Pingback: cialis pills online

  196. Pingback: best online pharmacies no prescription

  197. Pingback: viagra without a doctor prescription canada

  198. Pingback: clomid 100 mg tablet

  199. Pingback: amoxicillin no prescription

  200. Pingback: metformin without a script

  201. Pingback: can you order metformin online

發表迴響