Android提交数据到JavaWeb服务器实现登录

之前学习Android提交数据到php服务器没有成功,在看了两三个星期的视频之后,现在终于实现了与服务器的交互。虽然完成的不是PHP端的,但是在这个过程还是学到了不少东西的。现在我先来展示一下我的成果图。

思想是:服务器端模拟数据库中存在一条数据为:username=123,password=123的用户。如果Android端提交的数据是用户名和密码都为123的数据。就返回成功,否则就返回失败。

由于看的关于教程的视频都是跟javaweb进行交互,所以自己就尝试着搭建了javaweb服务器。在这个过程中,遇到了几个问题:

一、tomcat与javaweb的位数不一致,32位的javaweb不能在64位的tomcat上面运行。

二、tomcat默认的端口号是8080.但是运行的时候此端口被占用。所以就在web.xml文件中更改了端口号。

解决了javaweb上面的问题就是写一个接口给Android端。

先创建一个web工程,然后创建一个Servlet。

package com.itcast.Login;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public Login(){
		super();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("QQ号:"+username);
		System.out.println("密码:"+password);
		if ("123".equals(username)&&"123".equals(password)) {
			System.out.println("登录成功");
			response.getOutputStream().write("登录成功".getBytes("utf-8"));
			
		}else {
			System.out.println("登录失败");
			response.getOutputStream().write("登录失败".getBytes("utf-8"));
			
		}
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		
	}

}

  然后利用get方式访问服务器得到的结果是:

Android端的代码为:

package com.itheima.login;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

public class LoginService {
    public static String loginByPost(String username,String password){    
        try {
            String path = "http://192.168.1.101:8090/Java/servlet/Login";
            URL url = new URL(path);
            HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            String data = "username="+URLEncoder.encode(username)+"&password="
                    +URLEncoder.encode(password);
            System.out.println(data);
            conn.setRequestProperty("Content=Type", "application/x-wwww-form-urlencoded");
            conn.setRequestProperty("Content-length", data.length()+"");
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            int code = conn.getResponseCode();
            System.out.println(code);
            if (code == 200) {
                InputStream is = conn.getInputStream();
                String text = StreamTools.readInputStream(is);
                return text;
            }else {
                return null;
            }
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("111111");
        } catch (ProtocolException e) {
            System.out.println("222222");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("33333");
            e.printStackTrace();
        }
        return null;
    }
}

运行的结果为:

原文地址:https://www.cnblogs.com/kangyaping/p/5565077.html