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();
        }

    }
}

不要忘记权限:

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

 

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());
    }
%>

 

 

posted @ 2013-11-16 17:11  无忧之路  阅读(1065)  评论(0编辑  收藏  举报
无忧之路