Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 串 Tomcat(一):登入功能的客戶端網路實作

最近在做一个客戶端登入功能,最後選了 Tomcat 當 servlet 服務器、MySQL 當資料庫。今天先把客戶端寫完,也就是 Android 的網路編程部分;服務器端留到明天再說,今天有點累了。

相關閱讀:Android Tomcat 的應用之服務器部分 https://www.linuxidc.com/Linux/2012-03/55916.htm

版面配置

登入畫面很傳統:兩個輸入框加兩個按鈕,用 TableLayout 排成兩列,最後放一列按鈕。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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。把 GET/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 = "https://222.20.60.132:8080/WebRoot/";

    public static HttpGet getHttpGet(String url) {
        return new HttpGet(url);
    }

    public static HttpPost getHttpPost(String url) {
        return new HttpPost(url);
    }

    public static HttpResponse getHttpResponse(HttpGet request)
            throws ClientProtocolException, IOException {
        return new DefaultHttpClient().execute(request);
    }

    public static HttpResponse getHttpResponse(HttpPost request)
            throws ClientProtocolException, IOException {
        return new DefaultHttpClient().execute(request);
    }

    /** 送出 Post 請求並取回結果 */
    public static String queryStringForPost(String url) {
        HttpPost request = getHttpPost(url);
        return queryStringForPost(request);
    }

    public static String queryStringForPost(HttpPost request) {
        String result = null;
        try {
            HttpResponse response = 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 request = getHttpGet(url);
        String result = null;
        try {
            HttpResponse response = 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;
    }
}

值得注意的兩個細節:一是務必判斷 HTTP 狀態碼是否為 200,否則很容易把錯誤頁當成正常回應解析;二是例外要收斂成使用者看得懂的訊息,而不是把 stack trace 直接丟到畫面上。另外,真實專案裡把網路請求放在主執行緒會直接觸發 ANR,記得移到背景執行緒。

Copyright © Linux教程網 All Rights Reserved