zoukankan      html  css  js  c++  java
  • loginactivity

    package com.example.phonefragment;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;

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

    public class LoginActivity extends Activity {

    Handler handler;

    EditText username;
    EditText password;
    Button btn_login;
    Button btn_changepwd;
    Button btn_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    handler = new Handler();
    username = (EditText) findViewById(R.id.edt_loginid);
    password = (EditText) findViewById(R.id.edt_loginpassword);
    btn_login = (Button) findViewById(R.id.btn_login);
    btn_changepwd = (Button) findViewById(R.id.btn_login_forgetpassword);
    btn_register = (Button) findViewById(R.id.btn_login_register);

    btn_login.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    String name = username.getText().toString();
    String pwd = password.getText().toString();

    String url = "http://192.168.1.108:3000/users/queryById";

    LoginThread httpThread = new LoginThread(url, name, pwd);
    httpThread.start();
    }
    });

    btn_changepwd.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    intent.setClass(LoginActivity.this, ChangePwdActivity.class);
    startActivity(intent);
    }
    });

    btn_register.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    intent.setClass(LoginActivity.this, ChangePwdActivity.class);
    startActivity(intent);
    }
    });
    }

    class LoginThread extends Thread {

    String str;

    String url;

    String username;

    String password;

    public LoginThread(String url, String username, String password) {
    // TODO Auto-generated constructor stub
    this.url = url;
    this.username = username;
    this.password = password;
    }

    public void doGet() {
    HttpClient httpClient = new DefaultHttpClient();
    // 注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊
    String url = this.url + "?tel=" + username + "&pwd=" + password;
    // 第二步:创建代表请求的对象,参数是访问的服务器地址
    HttpGet httpGet = new HttpGet(url);
    try {
    // 第三步:执行请求,获取服务器发还的相应对象
    HttpResponse response = httpClient.execute(httpGet);
    // 第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
    if (response.getStatusLine().getStatusCode() == 200) {
    // 第五步:从相应对象当中取出数据,放到entity当中
    HttpEntity entity = response.getEntity();
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(entity.getContent()));
    StringBuffer sb = new StringBuffer();
    while ((str = reader.readLine()) != null) {
    sb.append(str);
    }
    str = sb.toString();
    if (str.trim().equals("empty")) {
    // 用户登录失败
    handler.post(new Runnable() {

    @Override
    public void run() {
    // TODO Auto-generated method stub
    Toast.makeText(LoginActivity.this, "账号不存在",
    Toast.LENGTH_LONG).show();
    }
    });
    } else {
    if (str.trim().equals("ok")) {
    // 用户登录成功
    Intent intent = new Intent();
    intent.setClass(LoginActivity.this,
    MainActivity.class);
    Bundle bundle = new Bundle(); // 创建Bundle对象
    bundle.putString("tel", username); // 装入数据
    intent.putExtras(bundle); // 把Bundle塞入Intent里面
    startActivity(intent);
    LoginActivity.this.finish();
    } else {
    handler.post(new Runnable() {

    @Override
    public void run() {
    // TODO Auto-generated method stub
    Toast.makeText(LoginActivity.this,
    "账号或密码错误", Toast.LENGTH_LONG)
    .show();
    }
    });
    }
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public void doPost() {
    HttpClient httpClient = new DefaultHttpClient();
    String httpUrl = this.url;
    HttpPost request = new HttpPost(httpUrl);
    // 传递参数
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tel", username));
    params.add(new BasicNameValuePair("pwd", password));
    HttpResponse response;
    HttpEntity entity;
    try {
    entity = new UrlEncodedFormEntity(params, "UTF-8");
    request.setEntity(entity);
    response = httpClient.execute(request);
    // 如果返回状态为200,获得返回的结果
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    String str = EntityUtils.toString(response.getEntity());
    if (str.trim().equals("ok")) {
    // 用户登录成功
    Intent intent = new Intent();
    intent.setClass(LoginActivity.this, MainActivity.class);
    startActivity(intent);
    LoginActivity.this.finish();
    } else {
    // 用户登录失败
    handler.post(new Runnable() {

    @Override
    public void run() {
    // TODO Auto-generated method stub
    Toast.makeText(LoginActivity.this, "账号或密码错误",
    Toast.LENGTH_SHORT).show();
    }
    });
    }
    }
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    @Override
    public void run() {
    // TODO Auto-generated method stub
    doGet();
    }
    }
    }

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
    android:id="@+id/img_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:contentDescription="@string/login_in"
    android:src="@drawable/user" />

    <LinearLayout
    android:id="@+id/lin_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_user"
    android:layout_marginTop="20dp"
    android:background="#ffffff"
    android:orientation="vertical" >

    <EditText
    android:id="@+id/edt_loginid"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginLeft="20dp"
    android:background="@null"
    android:hint="@string/username"
    android:textColorHint="#CDCDC1"
    android:textSize="16sp" />

    <LinearLayout
    android:id="@+id/loginline"
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:layout_marginLeft="14dp"
    android:layout_marginRight="14dp"
    android:background="#D7D7D7"
    android:orientation="vertical" >
    </LinearLayout>

    <EditText
    android:id="@+id/edt_loginpassword"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginLeft="20dp"
    android:background="@null"
    android:hint="@string/password"
    android:inputType="textPassword"
    android:textColorHint="#CDCDC1"
    android:textSize="16sp" />
    </LinearLayout>

    <Button
    android:id="@+id/btn_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lin_login"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/btnbg"
    android:includeFontPadding="false"
    android:text="@string/login_in"
    android:textColor="#ffffff" />

    <LinearLayout
    android:id="@+id/lin_login_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_below="@+id/btn_login"
    android:layout_marginTop="5dp"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <Button
    android:id="@+id/btn_login_forgetpassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left|center_vertical"
    android:layout_weight="1"
    android:background="#00000000"
    android:text="@string/forget_password"
    android:textColor="#00AFEF"
    android:textSize="14sp"
    style="?android:attr/buttonBarButtonStyle" />

    <Button
    android:id="@+id/btn_login_register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right|center_vertical"
    android:layout_weight="1"
    android:background="#00000000"
    android:text="@string/register"
    android:textColor="#00AFEF"
    android:textSize="14sp"
    style="?android:attr/buttonBarButtonStyle" />

    </LinearLayout>

    </RelativeLayout>

  • 相关阅读:
    numpy-tutorial
    Pandas 数据分析资料
    python3 创建虚拟环境
    机器学习中的评价指标--02
    机器学习中的评价指标--01
    pytest 测试框架
    Ubuntu 添加删除用户
    VSCODE 设置护眼颜色
    信息熵、交叉熵、KL散度等等
    深度学习优化方法演变和公式理解
  • 原文地址:https://www.cnblogs.com/lhang55/p/6529927.html
Copyright © 2011-2022 走看看