您的位置:首页 > 理论基础 > 计算机网络

从网络读取数据达到登录和注册的功能

2016-08-07 15:43 423 查看
一、使用Bmob网站,进行网络存储

二、登录界面的界面



三、布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#ffffffff"

    android:orientation="vertical"

    tools:context=".LoginActivity" >

    <ImageView

        android:id="@+id/imageview_login_logo"

        android:layout_width="72dp"

        android:layout_height="72dp"

        android:layout_gravity="center_horizontal"

        android:layout_marginTop="18dp"

        android:background="@drawable/logo" />

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:paddingLeft="5dp"

        android:text="用户名"

        android:textColor="#ff1196db"

        android:textSize="12sp" />

    <EditText

        android:id="@+id/edittext_login_user"

        android:layout_width="match_parent"

        android:layout_height="36dp"

        android:background="@drawable/line"

        android:hint="用户名:"

        android:paddingLeft="5dp"

        android:textColor="#ff000000"

        android:textSize="15sp" />

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="12dp"

        android:paddingLeft="5dp"

        android:text="密码:"

        android:textColor="#ff1196db"

        android:textSize="12sp" />

    <EditText

        android:id="@+id/edittext_login_pwd"

        android:layout_width="match_parent"

        android:layout_height="36dp"

        android:background="@drawable/line"

        android:hint="密码"

        android:password="true"

        android:paddingLeft="5dp"

        android:textColor="#ff000000"

        android:textSize="15sp" />

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_margin="12dp"

        android:orientation="horizontal" >

        <CheckBox

            android:id="@+id/checkbox_login_forgetpwd"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:button="@drawable/selector_checkbox"

            android:gravity="center"

            android:text="记住密码"

            android:textColor="#ff1196db" />

        <CheckBox

            android:id="@+id/checkbox_login_autologin"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:button="@drawable/selector_checkbox"

            android:gravity="center"

            android:text="自动登录"

            android:textColor="#ff1196db" />

    </LinearLayout>

    <Button

        android:id="@+id/button_login_login"

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:layout_marginLeft="12dp"

        android:layout_marginRight="12dp"

        android:background="@drawable/selector_login_btn"

        android:text="登    录"

        android:textColor="#ffffffff"

        android:textSize="18dp" />

    <RelativeLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:padding="12sp" >

        <Button

            android:id="@+id/button_login_cannotLogin"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentBottom="true"

            android:layout_alignParentLeft="true"

            android:background="#00000000"

            android:text="无法登录?"

            android:textColor="#ff1196db"

            android:textSize="15sp" />

        <Button

            android:id="@+id/button_login_registerUser"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentBottom="true"

            android:layout_alignParentRight="true"

            android:background="#00000000"

            android:text="注册新用户"

            android:textColor="#ff1196db"

            android:textSize="15sp" />

    </RelativeLayout>

</LinearLayout>

四、java代码

除了从网络提取数据,还有各种细节的也可以一下

功能:

1.保存用户名和密码

2.自动登录

3.从网络检查用用户是否存在

public class LoginActivity extends Activity {
private Button login,registerUser,cannotLogin;
private EditText user,pwd;
private CheckBox forgetPwd,autoLogin;
private Dialog dialog ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bmob.initialize(this, "b527798f489b995401c9498d59455455");
setContentView(R.layout.activity_login);
initialize();
setLoginButton();
setRegisterUser();
setAutoLogin();
}
//自动登录按键
private void setAutoLogin() {

autoLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(autoLogin.isChecked()){
forgetPwd.setChecked(true);
}
}
});
}
private void initialize() {
forgetPwd = (CheckBox) findViewById(R.id.checkbox_login_forgetpwd);
autoLogin = (CheckBox) findViewById(R.id.checkbox_login_autologin);
login = (Button) findViewById(R.id.button_login_login);
registerUser = (Button) findViewById(R.id.button_login_registerUser);
cannotLogin = (Button) findViewById(R.id.button_login_cannotLogin);
user = (EditText) findViewById(R.id.edittext_login_user);
pwd = (EditText) findViewById(R.id.edittext_login_pwd);
SharedPreferences sp = getSharedPreferences("key", Context.MODE_PRIVATE);
boolean checked = sp.getBoolean("ischeck", false);
boolean autoLogin_check = sp.getBoolean("autoLogin", false);
autoLogin.setChecked(autoLogin_check);
Log.i("TAG:autoLogin_check1", "autoLogin_check="+autoLogin_check);
//判断是否自动登录
if(autoLogin.isChecked()){
dialog = DialogManage.showRunLogin(LoginActivity.this);
dialog.show();
startActivity(new Intent(LoginActivity.this,MainActivity.class));
finish();
}
Log.i("TAG:check1", "check="+checked);
if(checked){
user.setText(sp.getString("user_name", ""));
pwd.setText(sp.getString("user_pwd", ""));
forgetPwd.setChecked(true);
}else{
user.setText(sp.getString("user_name", ""));
forgetPwd.setChecked(false);
}
}
private void setLoginButton() {
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
BmobQuery<Users> query = new BmobQuery<Users>();
query.addWhereContains("loginUser", user.getText().toString());
query.findObjects(LoginActivity.this, new FindListener<Users>() {

@Override
public void onSuccess(List<Users> users) {
SharedPreferences sp = getSharedPreferences("key", Context.MODE_PRIVATE);
Editor editor = sp.edit();
String user_name = user.getText().toString();
String user_pwd = pwd.getText().toString();
boolean frogetPwd_checked = forgetPwd.isChecked();
boolean autoLogin_checked = autoLogin.isChecked();
if(users.size()>0){
if(user.getText().toString().equals(users.get(0).getLoginUser())&&
pwd.getText().toString().equals(users.get(0).getLoginPwd())){
if(frogetPwd_checked){
if(autoLogin_checked){
editor.putString("user_name", user_name);
editor.putString("user_pwd", user_pwd);
editor.putBoolean("ischeck", true);
editor.putBoolean("autoLogin", true);
}else{
editor.clear();
editor.putString("user_name", user_name);
editor.putString("user_pwd", user_pwd);
editor.putBoolean("ischeck", true);
editor.putBoolean("autoLogin", false);
}
}else{
editor.clear();
editor.putString("user_name", user_name);
editor.putBoolean("isckeck", false);
}
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
intent.putExtra("loginName", user_name);
intent.putExtra("loginPwd", user_pwd);
intent.putExtra("isChecked", true);
startActivity(intent);
editor.putBoolean("isLogin", true);
editor.commit();
finish();
}
}else if(user.getText().toString().equals(users.get(0).getLoginUser())){
pwd.setText("");
pwd.setError("密码错误,请检查是否输入正解,重新输入");
}else{
user.setError("没有此用户,请先注册");
}
}

@Override
public void onError(int arg0, String arg1) {
Log.i("TAG:erro", arg1);
}
});
}
});
}
//注册新用户点击事件
private void setRegisterUser() {
registerUser.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
registerUser.setTextColor(Color.parseColor("#ffafafaf"));
break;
case MotionEvent.ACTION_UP:
registerUser.setTextColor(Color.parseColor("#ff1196db"));
Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);
break;
}
return true;
}
});
}
private boolean checkLogin(String username,String userpwd,String path) {
boolean flag = false;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(path);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
NameValuePair name1 = new BasicNameValuePair("username",username);
NameValuePair name2 = new BasicNameValuePair("userpwd", userpwd);
pairs.add(name1);
pairs.add(name2);
HttpEntity entity;
try {
entity = new UrlEncodedFormEntity(pairs);
post.setEntity(entity);
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity entity2 = response.getEntity();
String jsonStr = EntityUtils.toString(entity2);
JSONObject jsonObject = new JSONObject(jsonStr);
String result = jsonObject.getString("result");
if(result.equals("ok")){
flag = true;
}else{
flag = false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}

private void loginHttp() {
final String path = "http://172.60.16.49:8080/WebTest/DoLogin";
final String user_name = user.getText().toString();
final String user_pwd = pwd.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
final boolean flag = 
checkLogin(user_name, user_pwd, path);
LoginActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if(flag){
startActivity(new Intent(LoginActivity.this,MainActivity.class));
}else{
Toast.makeText(LoginActivity.this, "请检查用户名密码是否正确", 1000).show();
}
}
});
}
}).start();
}
@Override
protected void onDestroy() {
super.onDestroy();
}

}

五、注册实现 

功能比较简单

功能界面





注册界面比较简单就不传代码了,直接传完成注册的代码了(注册的功能也在完成注册的);

实现的功能:

1.实现贴 自动递增加

2.比对验证码是否正确

public class RegisterSuccessActivity extends Activity {
private TextView user,pwd;
private Button jumpLogin;
private EditText auth;
private int count;
String rand;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bmob.initialize(RegisterSuccessActivity.this, "b527798f489b995401c9498d59455455");
setContentView(R.layout.activity_register_success);
initView();
statisty();
setListener();
}
private void initView() {
user = (TextView) findViewById(R.id.tv_registerSuceess_user);
pwd = (TextView) findViewById(R.id.tv_registerSuceess_pwd);
auth = (EditText) findViewById(R.id.et_registerSuceess_authcode);
jumpLogin = (Button) findViewById(R.id.button_registerSuceess_login);
Intent intent = getIntent();
rand = intent.getStringExtra("rand");
Log.i("TAG:rand", rand);
Toast.makeText(RegisterSuccessActivity.this, rand, Toast.LENGTH_LONG).show();
}
private void statisty() {
BmobQuery<Users> query = new BmobQuery<Users>();
query.addQueryKeys("objectId");
query.findObjects(RegisterSuccessActivity.this, new FindListener<Users>() {

@Override
public void onSuccess(List<Users> users) {
count = users.size();
Log.i("TAG:count", count+"");
int n = 100000;
count = n+(count++);
user.setText(String.valueOf(count));

Random random = new Random();
char[] chars = new char[8];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) (random.nextInt(26)+97);
}
String randPwd = String.valueOf(chars);
pwd.setText(randPwd);

}
@Override
public void onError(int arg0, String arg1) {

}
});
}
private void setListener(){
jumpLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(auth.getText().toString().equals(rand)){
Users users = new Users();
users.setLoginUser(user.getText().toString());
users.setLoginPwd(pwd.getText().toString());
users.save(RegisterSuccessActivity.this, new SaveListener() {
@Override
public void onSuccess() {
Log.i("TAG:success", "注册成功");
Intent intent = new Intent(RegisterSuccessActivity.this,MainActivity.class);
startActivity(intent);
}
@Override
public void onFailure(int arg0, String arg1) {
Log.i("TAG:error", arg1);
}
});
}else{
auth.setText("");
auth.setError("验证码不正确,请重新输入");
}
}
});
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐