Android 鏈接tomcat模擬登錄帳號

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame">

    <TextView
        android:text="請輸入帳號"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

    <EditText
        android:id="@+id/username"
        android:text="heyiyong"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />

    <TextView
        android:text="請輸入密碼"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

    <EditText
        android:text="123"
        android:inputType="textPassword"
        android:id="@+id/password"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />

    <Button
        android:onClick="click"
        android:text="登錄"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</LinearLayout>

 

package com.wuyou.submittoserver;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends ActionBarActivity {

    private static final int OK = 200;
    private EditText usernameEditText;
    private EditText passwrodEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        usernameEditText = (EditText) findViewById(R.id.username);
        passwrodEditText = (EditText) findViewById(R.id.password);
    }


    public void click(View view) {
        final String username = usernameEditText.getText().toString().trim();
        final String password = passwrodEditText.getText().toString().trim();
        //Android默認模擬器外部的地址爲10.0.2.2,而不是localhost和127.0.0.1
        final String serverPath = "http://10.0.2.2:8080/login.jsp";
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            //給出提示:帳號密碼不準爲空
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //使用GET方式請求服務器只能這樣
                        URL url = new URL(serverPath + "?username=" + username + "&password=" + password);
                        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                        httpURLConnection.setRequestMethod("GET");
                        httpURLConnection.setConnectTimeout(5000);
                        httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0");
                        int responseCode = httpURLConnection.getResponseCode();
                        if (200 == responseCode) {
                            InputStream inputStream = httpURLConnection.getInputStream();
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                            final String responseMsg = bufferedReader.readLine();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, responseMsg, Toast.LENGTH_LONG).show();
                                }
                            });
                        } else {
                            System.out.println("responseCode = " + responseCode);
                            //鏈接服務器出錯,錯誤代碼爲:responseCode 根據代碼值告訴用戶出錯的緣由
                            //....
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }
}

不要忘記權限:java

<uses-permission android:name="android.permission.INTERNET"/>android

 

jsp頁面爲:服務器

<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username = " + username);
    System.out.println("password = " + password);
    if("heyiyong".equals(username) && "123".equals(password)) {
        response.getOutputStream().write("login successful!".getBytes());
    } else {
        response.getOutputStream().write("wrong username or password.".getBytes());
    }
%>
相關文章
相關標籤/搜索