Android Tomcat 的应用之客户端部分


最近因为做一个客户端的登录部分,最后选择了使用Tomcat作为servlet服务器,MySQL作为数据库,今天就先写了一下客户端的部分,主要就是Android的网络编程部分,服务器端编程明天再写吧,今天有点累了。

相关阅读:Android Tomcat 的应用之服务器部分

首先是布局文件,如下:

  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.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <TableLayout >  
  13.         <TableRow >  
  14.             <TextView   
  15.                 android:id="@+id/tv_name"  
  16.                 android:layout_width="wrap_content"  
  17.                 android:layout_height="wrap_content"  
  18.                 android:text="@string/nameStr"/>  
  19.             <EditText   
  20.                 android:id="@+id/et_name"  
  21.                 android:layout_width="fill_parent"  
  22.                 android:layout_height="wrap_content"/>  
  23.         </TableRow>  
  24.         <TableRow >  
  25.             <TextView   
  26.                 android:id="@+id/tv_passwd"  
  27.                 android:layout_width="wrap_content"  
  28.                 android:layout_height="wrap_content"  
  29.                 android:text="@string/passwdStr"/>  
  30.             <EditText   
  31.                 android:id="@+id/et_passwd"  
  32.                 android:layout_width="fill_parent"  
  33.                 android:layout_height="wrap_content"  
  34.                 android:password="true"/>  
  35.         </TableRow>  
  36.         <TableRow >  
  37.             <Button   
  38.                 android:id="@+id/btn_confirm"  
  39.                 android:layout_width="wrap_content"  
  40.                 android:layout_height="wrap_content"  
  41.                 android:text="@string/btn_confirm"/>  
  42.             <Button   
  43.                 android:id="@+id/btn_cancel"  
  44.                 android:layout_width="wrap_content"  
  45.                 android:layout_height="wrap_content"  
  46.                 android:text="@string/btn_cancel"/>  
  47.         </TableRow>  
  48.     </TableLayout>  
  49.       
  50. </LinearLayout>  

然后就是进行网络编程部分了,肯定是要用到post方式,这个部分就做一个单独的工具类,大家看一下就明白:

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

相关内容