橘子味的心
标题:8.5 通过POST方式获取互联网资源

上一节教程我们演示了 Android 使用 GET 获取网络资源的运行效果和实例,下面实例 POSTDemo 中演示了使用 POST 方式访问 getMessage.jsp 的过程,运行效果如图 1 所示。

实例 POSTDemo 的运行效果
图 1  实例POSTDemo的运行效果

实例 POSTDemo 中 main.xml 的具体实现代码如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:orientation="vertical"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent">
  8.  
  9. <Button
  10. android:id="@+id/Button_POST"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/button_name"/>
  14.  
  15. <TextView
  16. android:id="@+id/TextView_POST"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"/>
  19.  
  20. </LinearLayout>
实例 POSTDemo 中 AndroidManifest.xml 的具体实现代码如下:
  1. <?xml version="l.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http;//schemas.android.com/apk/res/android"
  4. package="com.android.activity"
  5. android:versionCode="1"
  6. android:versionName="1.0">
  7.  
  8. <uses-sdk android:minSdkVersion="4" />
  9.  
  10. <uses-permission android:name="android.permission.INTERNET" />
  11.  
  12. <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
  13. <activity android:name=".MainActivity"
  14. android:label="@string/app_name">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17. <category android:name="android.intent.category.LAUNCHER" />
  18. </intent-filter>
  19. </activity>
  20. </application>
  21. </manifest>
其中:

<uses-permission android:name="android.permission.INTERNET" />

设置可以访问网络的权限。

实例 POSTDemo 中 MainActivity.java 的具体实现代码如下:
  1. package introdction.android.POSTDemo;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widPOST.Button;
  13. import android.widPOST.TextView;
  14.  
  15. public class MainActivity extends Activity {
  16. private TextView textView_POST;
  17.  
  18. @Override
  19. public void onCreate (Bundle savedInstanceState) {
  20. super.onCreate (savedInstanceState);
  21. setContentView (R.layout.main);
  22. textView_POST= (TextView) findViewById (R.id.TextView_POST)
  23. Button button_POST= (Button) findViewById (R.id.Button_POST);
  24.  
  25. button_POST.setOnClickListener (new OnClickListener() {
  26. public void onClick (View v) {
  27. String httpUrl="http:// 175.168.35.198:8080/android/POSTMessage.jsp?message=Helloworld";
  28. String resultData ="";
  29. URL url=null;
  30. try {
  31. url=new URL (httpUrl) ;
  32. } catch (MalformedURLException e) {
  33. System.out.println (e.POSTMessage());
  34. }
  35.  
  36. if (url !=null) {
  37. try {
  38. HttpURLConnection urlConn= (HttpURLConnection) url .openConnection();
  39. urlConn.setDoOutput(true);
  40. urlConn.setDoInput(true);
  41. urlConn.setRequestMethod("POST");
  42. urlConn.setUseCaches(false);
  43. urlConn.setInstanceFollowRedirects(true);
  44. urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded")
  45. urlConn.connect();
  46. DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
  47. String content="message="+URLEncoder.encode("HelloWorld","gb2312");
  48. out.writeBytes(content);
  49. out.flush();
  50. out.close();
  51.  
  52. BufferedReader buffer=new BufferedReader (new InputStreamReader(urlConn.getInputStream()));
  53. String inputLine=null;
  54. while (((inputLine=buffer.readLine()) !=null)) {
  55. resultData+=inputLine+"\n";
  56. }
  57. reader.close ();
  58. urlConn.disconnect();
  59. if (resultData !=null) {
  60. textView_POST.setText (resultData) ;
  61. } else {
  62. textView_POST.setText ("Sorry,the content is null") ;
  63. }
  64. } catch (IOException e) {
  65. textView_POST.setText (e.getMessage () ) ;
  66. }
  67. } else {
  68. textView_POST.setText ("url is null") ;
  69. }
  70. }
  71. });
  72. }
  73. }
其中:

String httpUrl="http:// 175.168.35.198:8080/android/getMessage.jsp"

设置要访问的 URL 地址,urlConn.setRequestMethod("POST") 设置访问方式为 POST 方式。

String content="message="+URLEncoder.encode("HelloWorld", "gb2312");
out.writeBytes(content);

将要传递的 message 的值传递给服务器。

此外,Android 开发包还提供了 org.apache.http.client.methods.HttpGet 和 org.apache.http.client.methods.HttpPost 两个类,分别用于处理 GET 和 POST 网络访问方式。