zoukankan      html  css  js  c++  java
  • changepwdactivity

    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 ChangePwdActivity extends Activity {

    Handler handler;

    EditText tel;
    EditText pwd;
    EditText pwdagain;
    Button btn_confirm;

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

    handler = new Handler();
    tel = (EditText) findViewById(R.id.edt_changepwdnum);
    pwd = (EditText) findViewById(R.id.edt_changepwnewpwd);
    pwdagain = (EditText) findViewById(R.id.edt_changepwnewpwdagain);
    btn_confirm = (Button) findViewById(R.id.btn_changePw);

    btn_confirm.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    String telString = tel.getText().toString();
    String pwdString = pwd.getText().toString();
    String pwdagainString = pwdagain.getText().toString();

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

    if (pwdString.equals(pwdagainString)) {
    ChangePwdThread httpThread = new ChangePwdThread(url,
    telString, pwdString);
    httpThread.start();
    } else {
    Toast.makeText(ChangePwdActivity.this, "两次输入密码不相同,请重新输入",
    Toast.LENGTH_SHORT).show();
    }
    }
    });
    }

    class ChangePwdThread extends Thread {

    String str;

    String url;

    String username;

    String password;

    public ChangePwdThread(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("ok")) {
    // 用户登录成功
    Intent intent = new Intent();
    intent.setClass(ChangePwdActivity.this,
    LoginActivity.class);
    startActivity(intent);
    ChangePwdActivity.this.finish();
    } else {
    // 用户登录失败
    handler.post(new Runnable() {
    @Override
    public void run() {
    // TODO Auto-generated method stub
    Toast.makeText(ChangePwdActivity.this,
    str, 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(ChangePwdActivity.this,
    LoginActivity.class);
    startActivity(intent);
    ChangePwdActivity.this.finish();
    } else {
    // 用户登录失败
    handler.post(new Runnable() {

    @Override
    public void run() {
    // TODO Auto-generated method stub
    Toast.makeText(ChangePwdActivity.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="match_parent"
    android:layout_height="match_parent" >

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

    <LinearLayout
    android:id="@+id/lin_changepassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_changePwUser"
    android:layout_marginTop="40dp"
    android:background="#ffffff"
    android:orientation="vertical" >

    <EditText
    android:id="@+id/edt_changepwdnum"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginLeft="20dp"
    android:background="@null"
    android:hint="@string/please_input_phonenumber"
    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>

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

    <EditText
    android:id="@+id/edt_changepwidentify"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_marginLeft="6dp"
    android:layout_weight="1"
    android:background="@null"
    android:hint="@string/please_input_identifyingcode"
    android:inputType="textPassword"
    android:textColorHint="#CDCDC1"
    android:textSize="16sp" />

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

    <LinearLayout
    android:id="@+id/loginlinetwo"
    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_changepwnewpwd"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginLeft="20dp"
    android:background="@null"
    android:hint="@string/please_input_newphonenumber"
    android:inputType="textPassword"
    android:textColorHint="#CDCDC1"
    android:textSize="16sp" />

    <LinearLayout
    android:id="@+id/loginlinethree"
    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_changepwnewpwdagain"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginLeft="20dp"
    android:background="@null"
    android:hint="@string/please_input_newphonenumberagain"
    android:inputType="textPassword"
    android:textColorHint="#CDCDC1"
    android:textSize="16sp" />
    </LinearLayout>

    <Button
    android:id="@+id/btn_changePw"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lin_changepassword"
    android:layout_margin="20dp"
    android:background="@drawable/btnbg"
    android:includeFontPadding="false"
    android:text="@string/confirm"
    android:textColor="#ffffff" />

    </RelativeLayout>

  • 相关阅读:
    40个你可能不知道的Python的特点和技巧
    Python Streaming实战2: Join的实现与数据过滤
    python + Streaming框架的MR实践与优化
    python编码最佳实践之总结
    Python自然语言处理系列之模拟退火算法
    Python自然语言处理
    机器学习实践中的 7 种常见错误
    玩转python主题模型程序库gensim
    Python自然语言处理
    glusterfs
  • 原文地址:https://www.cnblogs.com/lhang55/p/6530347.html
Copyright © 2011-2022 走看看