您的位置:首页 > 移动开发 > Android开发

Android端实现用户登录

2015-08-27 18:17 453 查看
首先是web端,新建一个servlet:Sign.java

和web端登录的servlet相似

package cn.edu.hpu.android;

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

import cn.edu.service.impl.CompetitorManager;
import cn.edu.service.impl.CompetitorManagerImpl;

public class Login extends HttpServlet {

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

doPost(request, response);

}

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

response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");

CompetitorManager cm = new CompetitorManagerImpl();
//得到Android端传来的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//调用登录方法,判断用户名、密码是否匹配
int flag = cm.checkLogin(username, password);
if(flag != 0){
response.getOutputStream().write("success".getBytes());
}
}
}
Android端,新建一个class:
package cn.edu.hpu.utils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class LoginService {

public static int LoginConnectWeb(String username,String password) throws Exception {
//String path = "http://10.48.57.119:8080/tennis_game/Login?username="+username+"&&password="+password;
String path = "http://192.168.0.21:8080/tennis_game/Login?username="+username+"&&password="+password;
//将path包装成一个URL对象
URL url = new URL(path);
//System.out.println(url);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时时间
conn.setReadTimeout(5000);
//设置请求方式
conn.setRequestMethod("GET");
//判断请求是否成功
if(conn.getResponseCode() == 200) {
//流的工具类,专门从流中读取数据(返回的是二进制数据)
InputStream inStream = conn.getInputSt
4000
ream();
//返回json解析数据
return parseJSON(inStream);
}
return 0;
}

private static int parseJSON(InputStream inStream) throws Exception {
//StreamTool一个输入输出流类
byte[] data = StreamTool.read(inStream);
String json = new String(data);
if(json.equals("success")) {
return 1;
}
else{
return 0;
}
}
}MainActivity.java
package com.example.login;

import cn.edu.hpu.utils.LoginService;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

private Button login;
private Button register;
private EditText name,pwd;
private CheckBox remember, login_auto;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
super.onCreate(savedInstanceState);

StrictMode.setThreadPolicy(new
StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(
new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

setContentView(R.layout.activity_main);

name = (EditText) findViewById(R.id.username);
pwd = (EditText) findViewById(R.id.password);
remember = (CheckBox) findViewById(R.id.remember);
login_auto = (CheckBox) findViewById(R.id.login_auto);

login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String username = name.getText().toString();
String password = pwd.getText().toString();

//利用SharedPreferences 保存用户的登录信息
SharedPreferences sp =getSharedPreferences("FILE", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("username", username);
editor.commit();

int s = 0;
try{
s = LoginService.LoginConnectWeb(username, password);
if(s == 1) {
Intent intent = new Intent(MainActivity.this,ZhuYeActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "账号或密码错误请重新输入!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

register = (Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml中代码较长且比较简单,故省略
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android web 登录 login