歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android編程學習:通過http請求與Apache通信(完整版)

post_datas.php

[php]
  1. <?php  
  2. @header("content-type:text/html; charset=gb2312");  
  3. echo $_REQUEST['user'];  
  4. echo $_REQUEST['passwd'];  
  5. echo $_REQUEST['para'];  
  6. ?>   

Mini2440Activity.java

[java]
  1. package sg131971.mini2440;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.ClientProtocolException;  
  10. import org.apache.http.client.HttpClient;  
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  12. import org.apache.http.client.methods.HttpPost;  
  13. import org.apache.http.impl.client.DefaultHttpClient;  
  14. import org.apache.http.message.BasicNameValuePair;  
  15. import org.apache.http.util.EntityUtils;  
  16.   
  17. import Android.app.Activity;  
  18. import android.os.Bundle;  
  19. import android.view.View;  
  20. import android.view.View.OnClickListener;  
  21. import android.widget.Button;  
  22.   
  23. public class Mini2440Activity extends Activity {  
  24.   
  25.     public class myButtonListener implements OnClickListener {  
  26.   
  27.         private HttpResponse response;  
  28.         private String tmpString;  
  29.   
  30.         public void onClick(View arg0) {  
  31.             // TODO Auto-generated method stub   
  32.             HttpClient httpclient = new DefaultHttpClient();  
  33.             HttpPost httppost = new HttpPost("http://192.168.1.123/post_datas.php");  
  34.   
  35.             try {  
  36.                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);  
  37.                 nameValuePairs.add(new BasicNameValuePair("user""root"));  
  38.                 nameValuePairs.add(new BasicNameValuePair("passwd","shiguang"));  
  39.                 nameValuePairs.add(new BasicNameValuePair("para","58402F01"));  
  40.                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  41.   
  42.                 response = httpclient.execute(httppost);  
  43.                 tmpString = EntityUtils.toString(response.getEntity());  
  44.                 System.out.print(tmpString);  
  45.             } catch (ClientProtocolException e) {  
  46.                 // TODO Auto-generated catch block   
  47.                 e.printStackTrace();  
  48.             } catch (IOException e) {  
  49.                 // TODO Auto-generated catch block   
  50.                 e.printStackTrace();  
  51.             }  
  52.         }  
  53.     }  
  54.   
  55.     private Button myButton;  
  56.   
  57.     /** Called when the activity is first created. */  
  58.     @Override  
  59.     public void onCreate(Bundle savedInstanceState) {  
  60.         super.onCreate(savedInstanceState);  
  61.         setContentView(R.layout.main);  
  62.   
  63.         myButton = (Button) findViewById(R.id.myButton);  
  64.         myButton.setOnClickListener(new myButtonListener());  
  65.     }  
  66. }  
main.xml

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <Button android:id="@+id/myButton"   
  8.         android:text="@string/myButton"   
  9.         android:layout_height="wrap_content"   
  10.         android:layout_width="91dp"   
  11.         android:layout_x="126dp"   
  12.         android:layout_y="70dp"  
  13.         ></Button>  
  14. </AbsoluteLayout>  
AndroidManifest.xml [html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="sg131971.mini2440"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="4" />  
  7.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>   
  8.     <uses-permission android:name="android.permission.INTERNET" />   
  9.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  10.         <activity android:name=".Mini2440Activity"  
  11.                   android:label="@string/app_name">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.                 <category android:name="android.intent.category.LAUNCHER" />  
  15.             </intent-filter>  
  16.         </activity>  
  17.   
  18.     </application>  
  19. </manifest>  
下面兩行一定不能少,否則你的程序是不能使用網絡的。

[html]
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>   
  2. <uses-permission android:name="android.permission.INTERNET" />  
仿真結果:



Copyright © Linux教程網 All Rights Reserved