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

HTTP 通信中可以使用 GET 和 POST 方式,GET 方式可以获取静态页面,也可以把参数放在 URL 字符后面传递给服务器。

例如地址“http://175.168.35.198:8080/android/getMessage.jsp?message=Helloworld”就是使用 GET 方式,在 URL 中,“?”后面直接加入参数 message 的信息,而 POST 方式的参数是放在 HTTP 请求中的,不会直接出现在 URL 中。

与 GET 类似,POST 参数也是被 URL 编码的。然而,两者已经有了很多不同:

POST GET
服务器传送数据 服务器获取数据
数据放置在 HTML HEADER 内提交 URL 提交数据,数据在 URL 中可以看到
服务器端用 Request.Form 获取提交的数据 服务器端用 Request.QueryString 获取变量的值
POST 不限制提交数据大小 提交的数据最多只能有 1024 字节
参数不会显示在地址栏上 参数会显示在地址栏上

由此,如果这些数据是中文数据而且是非敏感数据,那么使用 GET 方式;如果用户输入的数据不是中文字符而且包含敏感数据,那么还是使用 POST 方式为好。

HttpURLConnection 默认的访问方式为 Get,以 POST 方式获取网页数据时需要使用 setRequestMethod 方法设置访问方式为 POST。

在 TOMCAT 根目录下的“webapps\android”目录下建立 getMessage.jsp 文件作为网络服务资源文件,该文件代码如下:
  1. <% @page language="java" import="java.util.*" pageEncoding="gb2312"%>
  2.  
  3. <HTTP>
  4. <HEAD>
  5. <TITLE>Get-HTTP-MESSAGE</TITLE>
  6. </HEAD>
  7. <BODY>
  8. <%
  9. String message=request.getParameter ("message") ;
  10. String result=new String (message.getBytes ("iso-8859-1") ,"gb2312");
  11. out.println ("<Hl>Android-message:"+result+"</H>") ;
  12. %>
  13. </BODY>
  14. </HTML>
该文件从访问该文件的 request 中获取名为 message 的参数信息并在页面上显示出来。

GET 方式获取网页数据的实现方式和指定 URL 方式很相似,不同的是要在将要访问的地址后面加上要传递的参数。

实例 GETDemo 中演示了使用 GET 方式访问指定网页的过程,运行效果如图 1 所示。

实例GETDemo的演示过程
图 1  实例 GETDemo 的演示过程
 
实例 GETDemo 中 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_Get"
  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_Get"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"/>
  19.  
  20. </LinearLayout>
实例 GETDemo 中 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"/>

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

实例 GETDemo 中 MainActivity.java 的具体实现代码如下:
  1. package introdction.android.getDemo;
  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.widget.Button;
  13. import android.widget.TextView;
  14.  
  15. public class MainActivity extends Activity {
  16. private TextView textView_Get;
  17.  
  18. @Override
  19. public void onCreate (Bundle savedInstanceState) {
  20. super.onCreate (savedInstanceState) ; setContentView (R.layout.main) ;
  21. textView_Get= (TextView) findViewById (R.id.TextView_Get)
  22. Button button_Get= (Button) findViewById (R.id.Button_Get);
  23. button_Get.setOnClickListener (new OnClickListener () {
  24. public void onClick (View v) {
  25. String httpUrl="http:// 175.168.35.198:8080/android/getMessage.jsp?message=Helloworld";
  26. String resultData ="";
  27. URL url=null;
  28.  
  29. try {
  30. url=new URL (httpUrl) ;
  31. } catch (MalformedURLException e) {
  32. System.out.println (e.getMessage());
  33. }
  34.  
  35. if (url !=null) {
  36. try {
  37. HttpURLConnection urlConn= (HttpURLConnection) url .openConnection();
  38. InputStreamReader in=new InputStreamReader (urlConn .getlnputStream()) ;
  39. BufferedReader buffer=new BufferedReader (in) ;
  40. String inputLine=null;
  41.  
  42. while (((inputLine=buffer.readLine()) !=null)) {
  43. resultData+=inputLine+"\n";
  44. }
  45. in.close ();
  46. urlConn.disconnect();
  47. if (resultData !=null) {
  48. textView_Get.setText (resultData) ;
  49. } else {
  50. textView_Get.setText ("Sorry,the content is null");
  51. }
  52. } catch (IOException e) {
  53. textView_Get.setText (e.getMessage () ) ;
  54. }
  55. } else {
  56. textView_Get.setText ("url is null") ;
  57. }
  58. }
  59. });
  60. }
  61. }
其中:

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

设置要访问的网页的URL,“message=Helloworld”设置要传递的参数 message 的值为 Helloworld。