Android Tomcat 的应用之客户端部分
最近因为做一个客户端的登录部分,最后选择了使用Tomcat作为servlet服务器,MySQL作为数据库,今天就先写了一下客户端的部分,主要就是Android的网络编程部分,服务器端编程明天再写吧,今天有点累了。
首先是布局文件,如下:
<?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; } }最后就是在Activity里面实现功能了,也就是设置按钮的事件监听器,如下:
package com.chenlong12580.app.tomcat; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class TomcatActivity extends Activity { /** Called when the activity is first created. */ private EditText et_name, et_passwd; private Button btn_confirm, btn_cancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_name = (EditText)findViewById(R.id.et_name); et_passwd = (EditText)findViewById(R.id.et_passwd); btn_confirm = (Button)findViewById(R.id.btn_confirm); btn_cancel = (Button)findViewById(R.id.btn_cancel); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub finish(); } }); btn_confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String queryStr = "username=" + et_name.getText().toString() +"&password=" + et_passwd.getText().toString(); String urlStr = HttpUtil.BASE_URL+"servlet/tomcat?"+queryStr; String result = HttpUtil.queryStringForPost(urlStr); if(result != null) { showDialog("登录成功!"); }else { showDialog("登录失败!"); } } }); } private void showDialog(String str) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(str).setPositiveButton("确定", null); AlertDialog dialog = builder.create(); dialog.show(); } }现在还不能测试,等明天写好服务器端再测试吧!