ҹ Ŵ Դͼ
(͹ 2) HttpGet HttpPost
㹡¹ Ҩ

1. ͡ Files > New > Android Project
2. My App ǡ Next ǡ Finish
3. ÷Ѵ AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
4. activity_main.xml Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name :" />

    <EditText
        android:id="@+id/Name"
        android:layout_width="wrap_content"
        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="Age :" />

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

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

    	<Button
	        android:id="@+id/btnPostData"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="Submit" 
	        android:onClick="btn_postdata" />

        <Button
            android:id="@+id/btnGetData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:onClick="btn_getdata"
            android:text="Get Data" />

    </RelativeLayout>
    
</LinearLayout>
5. MainActivity.java Code
package com.example.myapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
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.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	
    @SuppressLint("NewApi")
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }        
    }
    
    // Post Ţ䫷
    public void btn_postdata(View v) {
    	String url = "http://192.168.1.36/myapp/insert.php";
    	
        final EditText txtName = (EditText)findViewById(R.id.Name); 
        final EditText txtAge = (EditText)findViewById(R.id.Age); 

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", txtName.getText().toString()));
        params.add(new BasicNameValuePair("age", txtAge.getText().toString()));
        
    	String resultServer  = webPost(url, params);

    	Toast.makeText(MainActivity.this, resultServer, Toast.LENGTH_LONG).show();
    }

	public String webPost(String url, List<NameValuePair> params) {
		String data = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();

			httpPost.setEntity(new UrlEncodedFormEntity(params, "tis620"));
			data = client.execute(httpPost, responseHandler);
		} catch (IOException e) { }
		
		return data;
	}

	// ѺŨҡ䫷
    public void btn_getdata(View v) {
    	String url = "http://192.168.1.36/myapp/getdata.php";
    	String resultServer  = webGet(url);

    	Toast.makeText(MainActivity.this, resultServer, Toast.LENGTH_LONG).show();
    }

	public String webGet(String url) {
		String data = "";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(url);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();

			data = client.execute(httpGet, responseHandler);
		} catch (IOException e) { }
		
		return data;
	}

}
© Copyrights 2000 - 2017 www.palthai.com. All rights reserved.