歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android平台向web應用get、post方式提交信息案例

Android平台向web應用get、post方式提交信息案例

日期:2017/3/1 10:45:03   编辑:Linux編程

1、效果圖展示


2、界面布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout 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. <LinearLayout
  8. android:orientation="horizontal"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:gravity="center"
  12. >
  13. <Button
  14. android:id="@+id/get"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="@string/get"
  18. />
  19. <Button
  20. android:id="@+id/post"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="@string/post"
  24. />
  25. </LinearLayout>
  26. <EditText
  27. android:id="@+id/show"
  28. android:layout_width="fill_parent"
  29. android:layout_height="fill_parent"
  30. android:editable="false"
  31. android:cursorVisible="false"
  32. android:gravity="top"
  33. />
  34. </LinearLayout>
3、改發送get、post請求的工具類,如下:
  1. public class GetPostUtil
  2. {
  3. /**
  4. * 向指定URL發送GET方法的請求
  5. *
  6. * @param url
  7. * 發送請求的URL
  8. * @param params
  9. * 請求參數,請求參數應該是name1=value1&name2=value2的形式。
  10. * @return URL所代表遠程資源的響應
  11. */
  12. public static String sendGet(String url, String params)
  13. {
  14. String result = "";
  15. BufferedReader in = null;
  16. try
  17. {
  18. String urlName = url + "?" + params;
  19. URL realUrl = new URL(urlName);
  20. // 打開和URL之間的連接
  21. URLConnection conn = realUrl.openConnection();
  22. // 設置通用的請求屬性
  23. conn.setRequestProperty("accept", "*/*");
  24. conn.setRequestProperty("connection", "Keep-Alive");
  25. conn.setRequestProperty("user-agent",
  26. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  27. // 建立實際的連接
  28. conn.connect();
  29. // 獲取所有響應頭字段
  30. Map<String, List<String>> map = conn.getHeaderFields();
  31. // 遍歷所有的響應頭字段
  32. for (String key : map.keySet())
  33. {
  34. System.out.println(key + "--->" + map.get(key));
  35. }
  36. // 定義BufferedReader輸入流來讀取URL的響應
  37. in = new BufferedReader(
  38. new InputStreamReader(conn.getInputStream()));
  39. String line;
  40. while ((line = in.readLine()) != null)
  41. {
  42. result += "\n" + line;
  43. }
  44. }
  45. catch (Exception e)
  46. {
  47. System.out.println("發送GET請求出現異常!" + e);
  48. e.printStackTrace();
  49. }
  50. // 使用finally塊來關閉輸入流
  51. finally
  52. {
  53. try
  54. {
  55. if (in != null)
  56. {
  57. in.close();
  58. }
  59. }
  60. catch (IOException ex)
  61. {
  62. ex.printStackTrace();
  63. }
  64. }
  65. return result;
  66. }
  67. /**
  68. * 向指定URL發送POST方法的請求
  69. *
  70. * @param url
  71. * 發送請求的URL
  72. * @param params
  73. * 請求參數,請求參數應該是name1=value1&name2=value2的形式。
  74. * @return URL所代表遠程資源的響應
  75. */
  76. public static String sendPost(String url, String params)
  77. {
  78. PrintWriter out = null;
  79. BufferedReader in = null;
  80. String result = "";
  81. try
  82. {
  83. URL realUrl = new URL(url);
  84. // 打開和URL之間的連接
  85. URLConnection conn = realUrl.openConnection();
  86. // 設置通用的請求屬性
  87. conn.setRequestProperty("accept", "*/*");
  88. conn.setRequestProperty("connection", "Keep-Alive");
  89. conn.setRequestProperty("user-agent",
  90. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
  91. // 發送POST請求必須設置如下兩行
  92. conn.setDoOutput(true);
  93. conn.setDoInput(true);
  94. // 獲取URLConnection對象對應的輸出流
  95. out = new PrintWriter(conn.getOutputStream());
  96. // 發送請求參數
  97. out.print(params);
  98. // flush輸出流的緩沖
  99. out.flush();
  100. // 定義BufferedReade r輸入流來讀取URL的響應
  101. in = new BufferedReader(
  102. new InputStreamReader(conn.getInputStream()));
  103. String line;
  104. while ((line = in.readLine()) != null)
  105. {
  106. result += "\n" + line;
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. System.out.println("發送POST請求出現異常!" + e);
  112. e.printStackTrace();
  113. }
  114. // 使用finally塊來關閉輸出流、輸入流
  115. finally
  116. {
  117. try
  118. {
  119. if (out != null)
  120. {
  121. out.close();
  122. }
  123. if (in != null)
  124. {
  125. in.close();
  126. }
  127. }
  128. catch (IOException ex)
  129. {
  130. ex.printStackTrace();
  131. }
  132. }
  133. return result;
  134. }
  135. }
如果需要發送get請求只要調用URLConnection的connect()方法去建立實際的連接即可;如果需要發送post請求,則需要獲取URLConnection的OutputStream,然後再向網絡中輸出請求參數,如以上程序!!!

4、activity程序代碼

  1. public class GetPostMain extends Activity
  2. {
  3. Button get , post;
  4. EditText show;
  5. @Override
  6. public void onCreate(Bundle savedInstanceState)
  7. {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. get = (Button) findViewById(R.id.get);
  11. post = (Button) findViewById(R.id.post);
  12. show = (EditText)findViewById(R.id.show);
  13. get.setOnClickListener(new OnClickListener()
  14. {
  15. @Override
  16. public void onClick(View v)
  17. {
  18. String response = GetPostUtil
  19. .sendGet("http://192.168.65.1:8080/abc/a.jsp" , null);
  20. show.setText(response);
  21. }
  22. });
  23. post.setOnClickListener(new OnClickListener()
  24. {
  25. @Override
  26. public void onClick(View v)
  27. {
  28. String response = GetPostUtil
  29. .sendPost("http://192.168.65.1:8080/abc/login.jsp"
  30. , "name=crazyit.org&pass=leegang");
  31. show.setText(response);
  32. }
  33. });
  34. }
  35. }
該程序所發送的get、post請求都是向本地局域網內:http://192/168.65.1:8080/abc應用下兩個頁面發送,這個應用都是部署在本機的web應用;
Copyright © Linux教程網 All Rights Reserved