登录界面和注册界面自己配置就好了
转载的那个网站忘记了,我修改了一下button响应的部分代码
我只判断了 用户密码为空和用户密码非空两种情况,实际用的话需配置另一个
MainActivity.java
package com.example.treasure.httpclient; import java.security.MessageDigest; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; 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.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.net.ConnectivityManager; import android.net.NetworkInfo.State; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.Settings; import android.support.annotation.MainThread; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private Button loginBtn; private Button registerBtn; private EditText inputUsername; private EditText inputPassword; private ProgressDialog mDialog; private String responseMsg = ""; private static final int REQUEST_TIMEOUT = 5*1000;//设置请求超时10秒钟 private static final int SO_TIMEOUT = 10*1000; //设置等待数据超时时间10秒钟 private static final int LOGIN_OK = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginBtn = (Button) findViewById(R.id.btnlogin); registerBtn = (Button) findViewById(R.id.btnregister); inputUsername = (EditText) findViewById(R.id.username); inputPassword = (EditText) findViewById(R.id.passtext); loginBtn.setOnClickListener(listener); registerBtn.setOnClickListener(listener); } private View.OnClickListener listener=new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnlogin: mDialog = new ProgressDialog(MainActivity.this); mDialog.setTitle("登陆"); mDialog.setMessage("正在登陆服务器,请稍后..."); mDialog.show(); Thread loginThread = new Thread(new LoginThread()); loginThread.start(); break; case R.id.btnregister: Intent intent = new Intent(); intent.setClass(MainActivity.this, RegisterActivity.class); startActivity(intent); break; } } }; private boolean loginServer(String userName, String password) { boolean loginValidate = false; //使用apache HTTP客户端实现 String urlStr = "http://www.zcstation.pw/html/android/confirm/login.php"; HttpPost request = new HttpPost(urlStr); //如果传递参数多的话,可以对传递的参数进行封装 List params = new ArrayList(); //添加用户名和密码 params.add(new BasicNameValuePair("userName",userName)); params.add(new BasicNameValuePair("password",password)); try { //设置请求参数项 request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpClient client = getHttpClient(); //执行请求返回相应 HttpResponse response = client.execute(request); //判断是否请求成功 if(response.getStatusLine().getStatusCode()==200) { loginValidate = true; //获得响应信息 responseMsg = EntityUtils.toString(response.getEntity(),"UTF-8"); } }catch(Exception e) { e.printStackTrace(); } return loginValidate; } //初始化HttpClient,并设置超时 public HttpClient getHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT); HttpClient client = new DefaultHttpClient(httpParams); return client; } //Handler Handler handler = new Handler() { public void handleMessage(Message msg) { switch(msg.what) { case 0: mDialog.cancel(); Toast.makeText(getApplicationContext(),responseMsg, Toast.LENGTH_SHORT).show(); inputUsername.setText(""); inputPassword.setText(""); Intent intent = new Intent(); intent.setClass(MainActivity.this, UserActivity.class); startActivity(intent); finish(); break; case 1: mDialog.cancel(); Toast.makeText(getApplicationContext(),"用户名或密码错误,请重新输入", Toast.LENGTH_SHORT).show(); inputUsername.setText(""); inputPassword.setText(""); break; case 2: mDialog.cancel(); Toast.makeText(getApplicationContext(), "URL验证失败", Toast.LENGTH_SHORT).show(); inputUsername.setText(""); inputPassword.setText(""); break; } } }; //LoginThread线程类 class LoginThread implements Runnable { @Override public void run() { String userName = inputUsername.getText().toString(); String password = inputPassword.getText().toString(); System.out.println("userName="+userName+":password="+password); //URL合法,但是这一步并不验证密码是否正确 boolean loginValidate = loginServer(userName, password); System.out.println("----------------------------bool is :"+loginValidate+"----------response:"+responseMsg); Message msg = handler.obtainMessage(); if(loginValidate) { if(responseMsg.equals("用户管理员已经登录成功!")) { msg.what = 0; handler.sendMessage(msg); }else if(responseMsg.equals("用户宿舍管理员已经登录成功!")) { msg.what = 0; handler.sendMessage(msg); }else if(responseMsg.equals("用户教师已经登录成功!")) { msg.what = 0; handler.sendMessage(msg); }else if(responseMsg.equals("用户学生已经登录成功!")) { msg.what = 0; handler.sendMessage(msg); }else { msg.what = 1; handler.sendMessage(msg); } }else { msg.what = 2; handler.sendMessage(msg); } } } }
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="treasure\n\n" android:id="@+id/title" android:textSize="25sp" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" android:layout_marginBottom="25dp" android:id="@+id/user" android:layout_toLeftOf="@+id/passtext" android:layout_below="@id/title"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/username" android:layout_alignBottom="@id/user" android:layout_toRightOf="@id/user" android:hint="username"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:id="@+id/pass" android:layout_alignBottom="@id/passtext"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/passtext" android:layout_below="@id/username" android:layout_toRightOf="@id/pass" android:hint="password"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\n" android:id="@+id/title1" android:textSize="25sp" android:gravity="center" android:layout_below="@id/passtext"/> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnlogin" android:background="#ff00d9" android:layout_below="@id/title1" android:layout_marginBottom="30dp" android:text="登录" android:layout_centerHorizontal="true"/> <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnregister" android:background="#0011ff" android:layout_below="@id/btnlogin" android:layout_marginBottom="30dp" android:text="注册" android:layout_centerHorizontal="true"/> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="\n\n" android:textSize="25sp" android:gravity="center_horizontal"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/con_text" android:gravity="center_horizontal"/> </LinearLayout>