Android Tomcat 的应用之客户端部分
Android Tomcat 的应用之客户端部分
最近因为做一个客户端的登录部分,最后选择了使用Tomcat作为servlet服务器,MySQL作为数据库,今天就先写了一下客户端的部分,主要就是Android的网络编程部分,服务器端编程明天再写吧,今天有点累了。
相关阅读:Android Tomcat 的应用之服务器部分
首先是布局文件,如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <TableLayout >
- <TableRow >
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/nameStr"/>
- <EditText
- android:id="@+id/et_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </TableRow>
- <TableRow >
- <TextView
- android:id="@+id/tv_passwd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/passwdStr"/>
- <EditText
- android:id="@+id/et_passwd"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:password="true"/>
- </TableRow>
- <TableRow >
- <Button
- android:id="@+id/btn_confirm"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_confirm"/>
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/btn_cancel"/>
- </TableRow>
- </TableLayout>
- </LinearLayout>
然后就是进行网络编程部分了,肯定是要用到post方式,这个部分就做一个单独的工具类,大家看一下就明白:
- package com.chenlong12580.app.tomcat;
- import java.io.IOException;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- public class HttpUtil {
- // 基础URL
- public static final String BASE_URL="http://222.20.60.132:8080/WebRoot/";
- // 获得Get请求对象request
- public static HttpGet getHttpGet(String url){
- HttpGet request = new HttpGet(url);
- return request;
- }
- // 获得Post请求对象request
- public static HttpPost getHttpPost(String url){
- HttpPost request = new HttpPost(url);
- return request;
- }
- // 根据请求获得响应对象response
- public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{
- HttpResponse response = new DefaultHttpClient().execute(request);
- return response;
- }
- // 根据请求获得响应对象response
- public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{
- HttpResponse response = new DefaultHttpClient().execute(request);
- return response;
- }
- // 发送Post请求,获得响应查询结果
- public static String queryStringForPost(String url){
- // 根据url获得HttpPost对象
- HttpPost request = HttpUtil.getHttpPost(url);
- String result = null;
- try {
- // 获得响应对象
- HttpResponse response = HttpUtil.getHttpResponse(request);
- // 判断是否请求成功
- if(response.getStatusLine().getStatusCode()==200){
- // 获得响应
- result = EntityUtils.toString(response.getEntity());
- return result;
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- result = "网络异常!";
- return result;
- } catch (IOException e) {
- e.printStackTrace();
- result = "网络异常!";
- return result;
- }
- return null;
- }
- // 获得响应查询结果
- public static String queryStringForPost(HttpPost request){
- String result = null;
- try {
- // 获得响应对象
- HttpResponse response = HttpUtil.getHttpResponse(request);
- // 判断是否请求成功
- if(response.getStatusLine().getStatusCode()==200){
- // 获得响应
- result = EntityUtils.toString(response.getEntity());
- return result;
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- result = "网络异常!";
- return result;
- } catch (IOException e) {
- e.printStackTrace();
- result = "网络异常!";
- return result;
- }
- return null;
- }
- // 发送Get请求,获得响应查询结果
- public static String queryStringForGet(String url){
- // 获得HttpGet对象
- HttpGet request = HttpUtil.getHttpGet(url);
- String result = null;
- try {
- // 获得响应对象
- HttpResponse response = HttpUtil.getHttpResponse(request);
- // 判断是否请求成功
- if(response.getStatusLine().getStatusCode()==200){
- // 获得响应
- result = EntityUtils.toString(response.getEntity());
- return result;
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- result = "网络异常!";
- return result;
- } catch (IOException e) {
- e.printStackTrace();
- result = "网络异常!";
- return result;
- }
- return null;
- }
- }
|
评论暂时关闭