Android Tomcat 的应用之客户端部分
3536 点击·0 回帖
![]() | ![]() | |
![]() | 最近因为做一个客户端的登录部分,最后选择了使用Tomcat作为servlet服务器,MySQL作为数据库,今天就先写了一下客户端的部分,主要就是Android的网络编程部分,服务器端编程明天再写吧,今天有点累了。 首先是布局文件,如下: [html] <?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> <?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方式,这个部分就做一个单独的工具类,大家看一下就明白: [java] 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; } } 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里面实现功能了,也就是设置按钮的事件监听器,如下: [java] 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(); } } 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(); } } 现在还不能测试,等明天写好服务器端再测试吧! | |
![]() | ![]() |