Android 利用HTTP Post模式來傳送與接收資料的範例

MainActivity.java的內容
=====================================================================
package com.example.http_test;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
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 android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;  //加入widget類別

  


public class MainActivity extends ActionBarActivity {
    
    Thread HttpThread;
    EditText ed1;
EditText ed2;
EditText ed3;
TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
   tv1 = (TextView)findViewById(R.id.textView4);
ed1 = (EditText)findViewById(R.id.editText1);
ed2 = (EditText)findViewById(R.id.editText2);
ed3 = (EditText)findViewById(R.id.editText3);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
//必須利用Handler來改變主執行緒的UI值
private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//msg.getData會傳回Bundle,Bundle類別可以由getString(<KEY_NAME>)取出指定KEY_NAME的值
            tv1.setText(msg.getData().getString("response"));
        }
    };
    
    //按鈕的Click事件
    public void btn_onClick(View v) {
   //產生新的HttpThread物件
    HttpThread myThread = new HttpThread();
//設定變數值
    myThread.MyName=ed1.getText().toString();
    myThread.MyMessage=ed2.getText().toString();
    myThread.Url=ed3.getText().toString();
//開始執行緒
        myThread.start();
    }
       
    
    
    //宣告一個新的類別並擴充Thread
class HttpThread extends Thread { 
//宣告變數並指定預設值
public String MyName = "NoData" ;
public String MyMessage = "Nodata" ;
public String Url = "http://192.168.1.49/test/test.php";
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            
//宣告一個新的Bundle物件,Bundle可以在多個執行緒之間傳遞訊息
            Bundle myBundle = new Bundle();
            
try {    
HttpClient client = new DefaultHttpClient();
       URI website = new URI(this.Url);
//指定POST模式
       HttpPost request = new HttpPost();
//POST傳值必須將key、值加入List<NameValuePair>
             List<NameValuePair> parmas = new ArrayList<NameValuePair>();
//逐一增加POST所需的Key、值
         parmas.add(new BasicNameValuePair("MyName",this.MyName));
         parmas.add(new BasicNameValuePair("MyMessage",this.MyMessage));
//宣告UrlEncodedFormEntity來編碼POST,指定使用UTF-8
         UrlEncodedFormEntity env = new UrlEncodedFormEntity(parmas,HTTP.UTF_8);
         request.setURI(website);
//設定POST的List
         request.setEntity(env);
         HttpResponse response = client.execute(request);
         HttpEntity resEntity = response.getEntity();
         if(resEntity != null){
          myBundle.putString("response",EntityUtils.toString(resEntity));                
       }else{
              myBundle.putString("response","Nothing");     
           }              
       
   Message msg = new Message();
                msg.setData(myBundle);  
                mHandler.sendMessage(msg);
} catch (Exception e) {
                e.printStackTrace();
            }
}
}        
}



activity_main.xml的內容
=======================================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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.http_test.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="訊息"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="網址"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="http://192.168.1.39/test/test.php"
        android:textSize="15dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="btn_onClick"
        android:text="確定" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="15dp" />

</LinearLayout>



test.php
=======================================================================
<?php
   //設定網頁的type及編碼,因為Android中使用了utf-8,所以這裡一定要設定,
   //否則回傳的中文資料會變成亂碼
   header("Content-Type:text/html; charset=utf-8");
   $MyName = $_POST["MyName"];
   $MyMessage = $_POST["MyMessage"];    
   echo "我的姓名是:" . $MyName  . "  傳送的訊息是:"  . $MyMessage ;
?>  



利用Plink替代Putty來建立Script