最近因為做一個用戶端的登入部分,最後選擇了使用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 {// 基礎URLpublic static final String BASE_URL="http://222.20.60.132:8080/WebRoot/";// 獲得Get請求對象requestpublic static HttpGet getHttpGet(String url){HttpGet request = new HttpGet(url); return request;}// 獲得Post請求對象requestpublic static HttpPost getHttpPost(String url){ HttpPost request = new HttpPost(url); return request;}// 根據請求獲得響應對象responsepublic static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{HttpResponse response = new DefaultHttpClient().execute(request);return response;}// 根據請求獲得響應對象responsepublic 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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfinish();}}); btn_confirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString 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(); }}
現在還不能測試,等明天寫好伺服器端再測試吧!