• 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());
        }
    %>
  • 相关阅读:
    30 张图详解操作系统总结!
    Redis配置与优化
    SpringBoot中使用@Async注解使用及其失效问题解决 规格严格
    Key exchange was not finished,connection is closed近期遇到这个错误sshd更新导致的 规格严格
    java SpringBoot注解@Async不生效的解决方法 规格严格
    Kafka Producer配置 规格严格
    SpringBoot项目迁移到tongweb 规格严格
    Spring Boot系列二 Spring @Async异步线程池用法总结 规格严格
    OutOfMemoryError: GC Overhead Limit Exceeded错误解析 规格严格
    SQL语句update set 嵌套 case when以及MyBatis中的写法 规格严格
  • 原文地址:https://www.cnblogs.com/wuyou/p/3426968.html
Copyright © 2020-2023  润新知