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

Android端用户登录

2015-08-27 18:35 537 查看
这个注册比较简单,没有对各个输入内容格式的判断。Android端注册需要的servlet和web端雷同,代码如下:
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.model.competitor;
import cn.edu.service.impl.CompetitorManager;
import cn.edu.service.impl.CompetitorManagerImpl;

public class Register 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");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

String username = request.getParameter("username");
String password = request.getParameter("password");
String realname = request.getParameter("realname");
//System.out.println(request.getParameter("Matchage"));
int Matchage = Integer.parseInt(request.getParameter("Matchage"));
String birth = request.getParameter("birth");
String address = request.getParameter("address");
String sex = request.getParameter("sex");
//实例化
CompetitorManager cm = new CompetitorManagerImpl();
competitor c = new competitor();
c.setUsername(username);
c.setPassword(password);
c.setRealname(realname);
c.setMatchage(Matchage);
c.setBirth(birth);
c.setAddress(address);
c.setSex(sex);

cm.addcompetitor(c);

response.getOutputStream().write("success".getBytes());
}
}
Android端RegisterActivity.java
package com.example.login;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class RegisterActivity extends Activity {

private ImageButton back;
private Button ok;
private EditText uname, pass, rname, mage, bir, add, s;
private String responseMsg;
ProgressDialog progressDialog;
Handler handler;
String path = "http://192.168.0.49:8080/tennis_game/Register";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

this.setContentView(R.layout.activity_register);

uname = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
rname = (EditText)findViewById(R.id.realname);
mage = (EditText)findViewById(R.id.Matchage);
bir = (EditText)findViewById(R.id.birth);
add = (EditText)findViewById(R.id.address);
s = (EditText)findViewById(R.id.sex);

ok = (Button)findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
registerFun();
}
});

//Handler
handler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == 0) {//注册成功,跳转页面
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, ZhuYeActivity.class);
startActivity(intent);
}else {
progressDialog.dismiss();//.............
Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
finish();
}
};//Handler结束

back = (ImageButton) findViewById(R.id.back);
back.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
startActivity(intent);
}
});

}//onCreate结束
//注册函数
public void registerFun() {
// TODO Auto-generated method stub
new Thread() {
public void run() {
String username = uname.getText().toString().trim();
String password = pass.getText().toString().trim();
String realname = rname.getText().toString().trim();
//因为web端Matchage为int型,所以要进行类型转换
int Matchage = Integer.parseInt(mage.getText().toString().trim());
String birth = bir.getText().toString().trim();
String address = add.getText().toString().trim();
String sex = s.getText().toString().trim();
//registerValidate 定义为Boolean型,表示注册是否合法
boolean registerValidate = registerToHost(username, password, realname, Matchage, birth, address, sex);
//调用Handler
Message msg = handler.obtainMessage();
if(registerValidate) {
if(responseMsg.equals("success")) {
msg.what = 0;
handler.sendMessage(msg);
}else {
msg.what = 1;
handler.sendMessage(msg);
}
}
}
}.start();
}//注册函数结束

//处理客户端与服务器之间的请求
private boolean registerToHost(String username, String password,
String realname, int matchage, String birth, String address,
String sex) {

boolean flag = false;
//建立HttpPost对象
HttpPost httpRequest = new HttpPost(path);
HttpResponse httpResponse = null;
//建立一个NameValuePair数组,用于存储传送的参数
//本类位于System.Data.dll中,名为:System.Data.Common.NameValuePair。主要用途是在DBConnectionString类中,解析ConnectionString时存储并串联Name/Value对
List<NameValuePair> params = new ArrayList<NameValuePair>();
if(!username.equals("")&&!password.equals("")&&!realname.equals("")&&!(matchage==0)&&!birth.equals("")&&!address.equals("")&&!sex.equals("")){
//添加参数
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("realname", realname));
String smatchage = matchage+"";
//System.out.println(smatchage+"jkjk");
params.add(new BasicNameValuePair("Matchage", smatchage));
params.add(new BasicNameValuePair("birth", birth));
params.add(new BasicNameValuePair("address", address));
params.add(new BasicNameValuePair("sex", sex));
}
try {
//设置请求参数
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//发送post,并返回一个httpResponse对象
httpResponse = new DefaultHttpClient().execute(httpRequest);

//处理从服务器端传来的数据
if(httpResponse.getStatusLine().getStatusCode() == 200){
flag = true;
System.out.println("Connection OK");
responseMsg  = EntityUtils.toString(httpResponse.getEntity());
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
xml中代码省略.......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android应用 注册