post_datas.php
[php]
- <?php
- @header("content-type:text/html; charset=gb2312");
- echo $_REQUEST['user'];
- echo $_REQUEST['passwd'];
- echo $_REQUEST['para'];
- ?>
Mini2440Activity.java
[java]
- package sg131971.mini2440;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- 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.util.EntityUtils;
-
- import Android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
-
- public class Mini2440Activity extends Activity {
-
- public class myButtonListener implements OnClickListener {
-
- private HttpResponse response;
- private String tmpString;
-
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://192.168.1.123/post_datas.php");
-
- try {
- List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
- nameValuePairs.add(new BasicNameValuePair("user", "root"));
- nameValuePairs.add(new BasicNameValuePair("passwd","shiguang"));
- nameValuePairs.add(new BasicNameValuePair("para","58402F01"));
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
-
- response = httpclient.execute(httppost);
- tmpString = EntityUtils.toString(response.getEntity());
- System.out.print(tmpString);
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
- private Button myButton;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- myButton = (Button) findViewById(R.id.myButton);
- myButton.setOnClickListener(new myButtonListener());
- }
- }
main.xml
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button android:id="@+id/myButton"
- android:text="@string/myButton"
- android:layout_height="wrap_content"
- android:layout_width="91dp"
- android:layout_x="126dp"
- android:layout_y="70dp"
- ></Button>
- </AbsoluteLayout>
AndroidManifest.xml
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="sg131971.mini2440"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="4" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permission android:name="android.permission.INTERNET" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Mini2440Activity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- </application>
- </manifest>
下面兩行一定不能少,否則你的程序是不能使用網絡的。
[html]
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permission android:name="android.permission.INTERNET" />
仿真結果: