歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Tomcat 的應用之客戶端部分

Android Tomcat 的應用之客戶端部分

日期:2017/3/1 10:27:24   编辑:Linux編程

最近因為做一個客戶端的登錄部分,最後選擇了使用Tomcat作為servlet服務器,MySQL作為數據庫,今天就先寫了一下客戶端的部分,主要就是Android的網絡編程部分,服務器端編程明天再寫吧,今天有點累了。

相關閱讀:Android Tomcat 的應用之服務器部分 http://www.linuxidc.com/Linux/2012-03/55916.htm

首先是布局文件,如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello" />
  10. <TableLayout >
  11. <TableRow >
  12. <TextView
  13. android:id="@+id/tv_name"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="@string/nameStr"/>
  17. <EditText
  18. android:id="@+id/et_name"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"/>
  21. </TableRow>
  22. <TableRow >
  23. <TextView
  24. android:id="@+id/tv_passwd"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="@string/passwdStr"/>
  28. <EditText
  29. android:id="@+id/et_passwd"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:password="true"/>
  33. </TableRow>
  34. <TableRow >
  35. <Button
  36. android:id="@+id/btn_confirm"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="@string/btn_confirm"/>
  40. <Button
  41. android:id="@+id/btn_cancel"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="@string/btn_cancel"/>
  45. </TableRow>
  46. </TableLayout>
  47. </LinearLayout>

然後就是進行網絡編程部分了,肯定是要用到post方式,這個部分就做一個單獨的工具類,大家看一下就明白:

  1. package com.chenlong12580.app.tomcat;
  2. import java.io.IOException;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.client.ClientProtocolException;
  5. import org.apache.http.client.methods.HttpGet;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.DefaultHttpClient;
  8. import org.apache.http.util.EntityUtils;
  9. public class HttpUtil {
  10. // 基礎URL
  11. public static final String BASE_URL="http://222.20.60.132:8080/WebRoot/";
  12. // 獲得Get請求對象request
  13. public static HttpGet getHttpGet(String url){
  14. HttpGet request = new HttpGet(url);
  15. return request;
  16. }
  17. // 獲得Post請求對象request
  18. public static HttpPost getHttpPost(String url){
  19. HttpPost request = new HttpPost(url);
  20. return request;
  21. }
  22. // 根據請求獲得響應對象response
  23. public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{
  24. HttpResponse response = new DefaultHttpClient().execute(request);
  25. return response;
  26. }
  27. // 根據請求獲得響應對象response
  28. public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{
  29. HttpResponse response = new DefaultHttpClient().execute(request);
  30. return response;
  31. }
  32. // 發送Post請求,獲得響應查詢結果
  33. public static String queryStringForPost(String url){
  34. // 根據url獲得HttpPost對象
  35. HttpPost request = HttpUtil.getHttpPost(url);
  36. String result = null;
  37. try {
  38. // 獲得響應對象
  39. HttpResponse response = HttpUtil.getHttpResponse(request);
  40. // 判斷是否請求成功
  41. if(response.getStatusLine().getStatusCode()==200){
  42. // 獲得響應
  43. result = EntityUtils.toString(response.getEntity());
  44. return result;
  45. }
  46. } catch (ClientProtocolException e) {
  47. e.printStackTrace();
  48. result = "網絡異常!";
  49. return result;
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. result = "網絡異常!";
  53. return result;
  54. }
  55. return null;
  56. }
  57. // 獲得響應查詢結果
  58. public static String queryStringForPost(HttpPost request){
  59. String result = null;
  60. try {
  61. // 獲得響應對象
  62. HttpResponse response = HttpUtil.getHttpResponse(request);
  63. // 判斷是否請求成功
  64. if(response.getStatusLine().getStatusCode()==200){
  65. // 獲得響應
  66. result = EntityUtils.toString(response.getEntity());
  67. return result;
  68. }
  69. } catch (ClientProtocolException e) {
  70. e.printStackTrace();
  71. result = "網絡異常!";
  72. return result;
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. result = "網絡異常!";
  76. return result;
  77. }
  78. return null;
  79. }
  80. // 發送Get請求,獲得響應查詢結果
  81. public static String queryStringForGet(String url){
  82. // 獲得HttpGet對象
  83. HttpGet request = HttpUtil.getHttpGet(url);
  84. String result = null;
  85. try {
  86. // 獲得響應對象
  87. HttpResponse response = HttpUtil.getHttpResponse(request);
  88. // 判斷是否請求成功
  89. if(response.getStatusLine().getStatusCode()==200){
  90. // 獲得響應
  91. result = EntityUtils.toString(response.getEntity());
  92. return result;
  93. }
  94. } catch (ClientProtocolException e) {
  95. e.printStackTrace();
  96. result = "網絡異常!";
  97. return result;
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. result = "網絡異常!";
  101. return result;
  102. }
  103. return null;
  104. }
  105. }
Copyright © Linux教程網 All Rights Reserved