Post data in andoird

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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 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";

    @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){
                    Thread thread = new Thread(new Runnable() { public void run() {
                        String r = sendPostDataToInternet();
                        if (r != null)
                            System.out.println(r);
                    }});
                    thread.start();
                }
            }
        }
    };

    private String 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){
                String strResult = EntityUtils.toString(httpResponse.getEntity());
                return strResult;
            } 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;
    }
}

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"/>
<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>
</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"
        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"
        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>

index.php

<?php
	echo $_POST['username']." ".$_POST['password'];
?>

Output
I/System.out: username password

Remark
lib: http-core-4.1.jar, httpclient-4.2.3.jar

匯入JAR後, 需要按 “Sync Project with Gradle Files"

103 thoughts on “Post data in andoird

  1. Pingback: viagra vs levitra

  2. Pingback: generic cialis india

  3. Pingback: cialis 5mg price

  4. Pingback: cialis 10mg

  5. Pingback: Viagra approved

  6. Pingback: Viagra jelly

  7. Pingback: cialis manufacturer coupon 2019

  8. Pingback: buy cialis canada

  9. Pingback: cialis price walmart

  10. Pingback: when will generic cialis be available

  11. Pingback: is there a generic for cialis

  12. Pingback: viagra for sale

  13. Pingback: viagra for sale

  14. Pingback: viagra generic

  15. Pingback: ed pills otc

  16. Pingback: ed meds online without doctor prescription

  17. Pingback: buy ed pills online

  18. Pingback: online pharmacy

  19. Pingback: online canadian pharmacy

  20. Pingback: cialis visa

  21. Pingback: levitra vardenafil

  22. Pingback: levitra for sale

  23. Pingback: online slots

  24. Pingback: virgin casino online nj

  25. Pingback: viagra online prescription free

  26. Pingback: real money casino

  27. Pingback: hollywood casino online real money

  28. Pingback: cialis online

  29. Pingback: installment loans

  30. Pingback: cash payday

  31. Pingback: loans online

  32. Pingback: viagra cost

  33. Pingback: cialis 5 mg

  34. Pingback: 20 cialis

  35. Pingback: new cialis

  36. Pingback: cialis buy

  37. Pingback: buy cialis

  38. Pingback: casino gambling

  39. Pingback: online casinos

  40. Pingback: rivers casino

  41. Pingback: casino game

  42. Pingback: buying viagra online

  43. Pingback: viagra generic

  44. Pingback: viagra cost

  45. Pingback: cialis generic

  46. Pingback: cialis buy cialis online

  47. Pingback: buy tadalafil online

  48. Pingback: buy cialis online cheap

  49. Pingback: online casino real money usa

  50. Pingback: real money casino online

  51. Pingback: buy cialis brand

  52. Pingback: Viagra jelly

  53. Pingback: is it legal to buy viagra online

  54. Pingback: cialis

  55. Pingback: no prescription cialis

  56. Pingback: viagra capsules online in india

  57. Pingback: price viagra in india

  58. Pingback: where can i buy levitra in uk

  59. Pingback: buy viagra tablets in india

  60. Pingback: viagra tablet online purchase

  61. Pingback: viagra canada prescription

  62. Pingback: cheap viagra canada free shipping

  63. Pingback: where to purchase viagra

  64. Pingback: brazilian cialis

  65. Pingback: Real viagra

  66. Pingback: buy viagra australia

  67. Pingback: Generic viagra canada

  68. Pingback: o cialis diario funciona

  69. Pingback: cialis online

  70. Pingback: dissertationhelpvfh.com

  71. Pingback: recommended essay writing service

  72. Pingback: https://customessaywriterbyz.com/

  73. Pingback: narrative essay help

  74. Pingback: thesis proposals

  75. Pingback: buy custom research paper

  76. Pingback: need help write my paper

  77. Pingback: research thesis

  78. Pingback: Discount viagra no rx

  79. Pingback: order cialis online canada

  80. Pingback: cialis 5mg

  81. Pingback: generic cialis india

  82. Pingback: best place to buy viagra online

  83. Pingback: viagra coupon

  84. Pingback: does viagra make you bigger

  85. Pingback: how to order cialis

  86. Pingback: 20mg cialis buy

  87. Pingback: cheap real cialis

  88. Pingback: cialis nex day delivery uk

  89. Pingback: receive cialis overnight

  90. Pingback: can i buy cialis online

  91. Pingback: cialis without prescriptions canada

  92. Pingback: cialis tadalafil portugal

  93. Pingback: buy cialis online viagra

  94. Pingback: buy cialis drug

  95. Pingback: buy cialis online generic

  96. Pingback: how to buy cialis online from canada

  97. Pingback: generic cialis daily pricing

  98. Pingback: buy cialis online generic

  99. Pingback: essay about helping others

  100. Pingback: purchase cialis online canadian

  101. Pingback: walmart pharmacy viagra

  102. Pingback: how long does cialis last

  103. Pingback: metformin hydrochloride 500 mg

發表迴響