您的位置:首页 > 其它

简单的采用post方式验证用户名和密码

2015-08-20 12:55 429 查看


设计思路:

1获取用户输入的用户名和密码,并判断是否为空

2将获取的用户名和密码,发送到服务器端,并进行验证

3获取返回的响应信息,并输出到用户界面

具体代码:
import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;
import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.text.TextUtils;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.webkit.WebView;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {

 private EditText mEtUserName;

 private EditText mEtUserPassword;

 private Button mBtnLogin;

 private WebView mWvShow;

 private StringBuffer mStrBuffer;
 private MessageHandler mHandler = new MessageHandler();
 private static final int INPUT_IS_EMPTY = 0;

 private static final int REQUST_SUCCESS = 1;

 private static final int REQUST_FAIL = 2;
 private static final String DEFAULT_PARAMS_ENCODING = "utf-8";

 private static final String MIME_TYPE = "text/html";
 @Override

 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);
  initView();

  mBtnLogin.setOnClickListener(this);
 }
 private void initView() {

  mEtUserName = (EditText) findViewById(R.id.mainAc_et_userName);

  mEtUserPassword = (EditText) findViewById(R.id.mainAc_et_userPassword);
  mBtnLogin = (Button) findViewById(R.id.mainAc_btn_login);

  mWvShow = (WebView) findViewById(R.id.mainAc_wv_show);

 }
 @Override

 public void onClick(View v) {

  // 获取用户输入的用户名和密码

  String userName = mEtUserName.getText().toString().trim();

  String userPassword = mEtUserPassword.getText().toString().trim();
  // 使用TextUtil 工具类,判断用户名和密码是否为空

  if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPassword)) {

   mHandler.sendEmptyMessage(INPUT_IS_EMPTY);

   return;

  }
  connectIntenet(userName, userPassword);
 }
 private void connectIntenet(final String userName, final String userPassword) {
  new Thread() {

   public void run() {

    OutputStream os = null;

    InputStream is = null;

    try {

     URL url = new URL(

       "http://192.168.15.200:8088/UserManager/longin");

     HttpURLConnection httpURLConnection = (HttpURLConnection) url

       .openConnection();

     // 设置httpURLConnection连接的属性

     httpURLConnection.setRequestMethod("POST");// 设置请求方式为POST

     httpURLConnection.setReadTimeout(8000);// 设置读取超时时间

     httpURLConnection.setConnectTimeout(8000);// 设置连接超时时间
     // 设置

     httpURLConnection.setDefaultUseCaches(false);// 设置是否使用缓冲区

     httpURLConnection.setDoInput(true);

     httpURLConnection.setDoOutput(true);

     // 打开连接

     httpURLConnection.connect();

     // 向服务器发送数据

     String requestStr = "username=" + userName + "&password="

       + userPassword;

     os = httpURLConnection.getOutputStream();

     os.write(requestStr.getBytes());

     os.flush();
     int code = httpURLConnection.getResponseCode();
     System.out.println(code);
     if (code == HttpURLConnection.HTTP_OK) {
      // 从服务器得到数据

      is = httpURLConnection.getInputStream();

      mStrBuffer = new StringBuffer();

      int length = 0;

      byte[] bytes = new byte[1024 * 1024];

      while ((length = is.read(bytes)) != -1) {

       mStrBuffer.append(new String(bytes, 0, length));

      }

      mHandler.sendEmptyMessage(REQUST_SUCCESS);
     } else {// 若连接诶不成功

      mHandler.sendEmptyMessage(REQUST_FAIL);
     }
    } catch (Exception e) {

     e.printStackTrace();

    } finally {

     if (os != null && is != null) {

      try {

       os.close();

       is.close();

      } catch (IOException e) {

       e.printStackTrace();

      }

     }
    }

   };

  }.start();
 }
 class MessageHandler extends Handler {

  @Override

  public void handleMessage(Message msg) {

   switch (msg.what) {

   case INPUT_IS_EMPTY:

    Toast.makeText(MainActivity.this, "用户名或密码不能为空",

      Toast.LENGTH_SHORT).show();
    break;

   case REQUST_SUCCESS:

    mWvShow.loadDataWithBaseURL(null, mStrBuffer.toString(),

      MIME_TYPE, DEFAULT_PARAMS_ENCODING, null);
    break;

   case REQUST_FAIL:

    Toast.makeText(MainActivity.this, "请求连接失败", Toast.LENGTH_SHORT)

      .show();

    break;
   }

布局文件:
<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:orientation="vertical"

    tools:context=".MainActivity" >
    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >
        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="用户名:" />
        <EditText

            android:id="@+id/mainAc_et_userName"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:singleLine="true" />

    </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >
        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="密    码:" />
        <EditText

            android:id="@+id/mainAc_et_userPassword"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:inputType="textPassword"

            android:singleLine="true" />

    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

        <Button

            android:id="@+id/mainAc_btn_login"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="登录" />

    </LinearLayout>

    <WebView

        android:id="@+id/mainAc_wv_show"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

</LinearLayout>

  }

 }

}

运行结果:

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