简易登陆界面的实现

今天实现了简易的登陆界面,通过与服务器的简单交互实现了登录的功能。

界面布局使用相对布局,不做过多讲解,实现界面如下

居中的实现时将用户名的标签添加  android:layout_centerVertical="true"属性



1、首先搭建服务器

创建servlet文件,方法的实现都放在doGet()中。

将客户端输入的用户名和密码传入服务器。

当用户名和密码都匹配时显示登陆成功,否则失败。

通过login.jsp测试


LoginServlet实现源码如下:

	request.setCharacterEncoding("UTF-8");
	String username = request.getParameter("userName");
	String password = request.getParameter("userpass");
	System.out.println(username+"***"+password);
		
	if(name.equals(username)&&"123".equals(password)){
		response.getOutputStream().write("成功".getBytes());
	}else {
		response.getOutputStream().write("失败".getBytes());
	}
request.setCharacterEncoding("UTF-8");用来解决部分客户端到服务器传输乱码的问题

2、根据不同的传输方式将用户名和密码传入服务器

首先监听登录按钮

确定输入的 用户名和密码不为空。

如果是通过get方法传送数据,则需要将传送的路径定义为String spec = "http://172.16.237.145:8080/video/login.do?userName="
+ userName + "&userpass=" + userPass;

建立客户端和服务器的连接

判断是否正确连接,并将结果通过装入Message,通过 handler方法在客户端上显示是否成功登陆。

具体getOper()方法如下

public void getOper(String userName, String userPass) {

		try {
			String spec = "http://172.16.237.145:8080/video/login.do?userName="
					+ userName + "&userpass=" + userPass;
			URL url = new URL(spec);
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.setRequestMethod("GET");
			urlConnection.setReadTimeout(5000);
			urlConnection
					.setRequestProperty(
							"User-Agent",
							"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");

			if (urlConnection.getResponseCode() == 200) {
				InputStream is = urlConnection.getInputStream();
				String result = StreamTools.streamToStr(is);
				System.out.println("返回结果"+result);
				Message msg = new Message();
				msg.what = CHANGETEXTVIEW;
				msg.obj = result;
				handler.sendMessage(msg);
				System.out.println(userName+"==="+userPass);

			} else {
				System.out.println("连接失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
如果通过post方法传输,思路相同

不同有以下几点:

spec路径为String spec = "http://172.16.237.145:8080/video/login.do";

在传输用户名和密码时使用data,与路径分开String data = "userName=" + userName + "&userpass="
+ userPass;

具体post方法如下:

public void postOper(String userName, String userPass){
		try {
			String spec = "http://172.16.237.145:8080/video/login.do";
			URL url = new URL(spec);
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			urlConnection.setRequestMethod("POST");
			String data = "userName=" + userName + "&userpass="
					+ userPass;
			//System.out.println(data);
			urlConnection.setReadTimeout(5000);
			urlConnection.setConnectTimeout(5000);

			urlConnection.setRequestProperty("Connection",
					"keep-alive");
			urlConnection.setRequestProperty("Content-Length",
					String.valueOf(data.getBytes().length));
			;
			urlConnection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");

			urlConnection
					.setRequestProperty(
							"User-Agent",
							"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");

			urlConnection.setDoInput(true);
			urlConnection.setDoOutput(true);

			OutputStream os = urlConnection.getOutputStream();

			os.write(data.getBytes());
			os.flush();
			if (urlConnection.getResponseCode() == 200) {
				InputStream is = urlConnection.getInputStream();
				String result = StreamTools.streamToStr(is);
				System.out.println("返回结果" + result);
				/*
				 * te_result.setText(result);
				 */
				Message msg = new Message();
				msg.what = CHANGETEXTVIEW;
				msg.obj = result;
				handler.sendMessage(msg);

			} else {
				System.out.println("连接失败");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

返回结果方法Handle使用内部类创建

具体代码如下:

private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			case CHANGETEXTVIEW:
				String result = (String) msg.obj;
				te_result.setText(result);
				System.out.println("---------------");
				break;
			default:
				break;
			}
		};
	};

监听事件代码如下:

public void login(View v) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch (id) {
		case R.id.logInBtn:
			final String userName = URLEncoder.encode(et_username.getText().toString(),"UTF-8");
			final String userPass = et_pass.getText().toString();
			if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPass)) {
				Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(this, "send", Toast.LENGTH_LONG).show();
				new Thread() {
					public void run() {
						getOper(userName, userPass);
						//postOper(userName, userPass);
					};
				}.start();

			}
			break;

		default:
			break;
		}
	}
final String userName = URLEncoder.encode(et_username.getText().toString(),"UTF-8");中encode方法的调用是为了解决get方法在传输时乱码的问题。

总体来说项目不是很难,但是需要很仔细,我在码的时候在post方法传入时总是传入空值,检查后得知为String data = "userName=" + userName + "&userpass="
+ userPass;重uerName和userpass少了两个“=”,这搞的我那个叫郁闷。

你可能感兴趣的:(简易登陆界面的实现)