1:服务端代码如下
<?php /** *登录成功就返回 1,否则返回 0 */ $REQUEST_METHOD=$_SERVER['REQUEST_METHOD']; if($REQUEST_METHOD=='GET'){ $name=$_GET['name']; $pwd=$_GET['pwd']; if($name=='yanshiying' && $pwd=='yanshiying'){ echo 1; }else{ echo 0; } }else if($REQUEST_METHOD=='POST'){ $name=$_POST['name']; $pwd=$_POST['pwd']; if($name=='yanshiying' && $pwd=='yanshiying'){ echo 1; }else{ echo 0; } } ?>
2:Android Client端。
(1):activity_main.xml
<RelativeLayout 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"> <ProgressBar android:id="@+id/pb_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" style="?android:attr/progressBarStyleLarge" android:visibility="gone"/> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username"/> <EditText android:id="@+id/et_pwd" android:layout_below="@id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword"/> <CheckBox android:id="@+id/cb_remember" android:layout_below="@id/et_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Remember me"/> <Button android:id="@+id/btn_login" android:layout_below="@id/cb_remember" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login"/> </RelativeLayout>
(2):UIHelper.java
为了更方便使用Toast,所以在这里自定义了一个类,进行了简单的封装。
public class UIHelper { private Context context=null; public UIHelper(Context context){ this.context=context; } public void ShortToast(String text){ Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); } public void LongToast(String text){ Toast.makeText(context, text, Toast.LENGTH_LONG).show(); } }
(3):NetHelper.java
此类主要和服务端进行网络交互。
public class NetHelper { public static String doLogin(String url) { String result=""; HttpClient client=new DefaultHttpClient(); HttpGet get=new HttpGet(url); try { HttpResponse response=client.execute(get); //服务端成功响应 if(response.getStatusLine().getStatusCode()==200){ //取得服务端返回的数据 result=EntityUtils.toString(response.getEntity()); } } catch (Exception e) { } return result; } }
(4):MainActivity.java 用户登录。
public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener { //用于保存用户名 public static String USER_NAME=""; private SharedPreferences sp=null; private EditText etName=null; private EditText etPwd=null; private CheckBox cbRemember=null; private Button btnLogin=null; private ProgressBar pbLogin=null; private boolean remember=false; private String name=""; private String pwd=""; private UIHelper uiHelper=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化UI initUI(); //读取用户登录信息 readSharedPreferences(); cbRemember.setOnCheckedChangeListener(this); btnLogin.setOnClickListener(this); } @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { if(cbRemember.isChecked()){ remember=true; }else{ remember=false; } } @Override public void onClick(View arg0) { name=etName.getText().toString(); pwd=etPwd.getText().toString(); if(doValidateForm()){ pbLogin.setVisibility(View.VISIBLE); //开启线程进行用户登录处理 new Thread(new LoginThread()).start(); } } private void initUI(){ sp=this.getSharedPreferences("account", MODE_PRIVATE); etName=(EditText)findViewById(R.id.et_name); etPwd=(EditText)findViewById(R.id.et_pwd); cbRemember=(CheckBox)findViewById(R.id.cb_remember); btnLogin=(Button)findViewById(R.id.btn_login); pbLogin=(ProgressBar)findViewById(R.id.pb_login); uiHelper=new UIHelper(this); } private void readSharedPreferences(){ String name=sp.getString("u_name", ""); String pwd=sp.getString("u_pwd", ""); etName.setText(name); etPwd.setText(pwd); if(!(name.equals("") &&pwd.equals(""))){ remember=true; cbRemember.setChecked(true); } } private void saveSharedPreferences(){ SharedPreferences.Editor editor=sp.edit(); if(remember){//记住用户登录信息 editor.putString("u_name", name); editor.putString("u_pwd", pwd); editor.commit(); }else{ editor.clear(); editor.commit(); } } //校验用户字段 private boolean doValidateForm(){ if(name.length()<1){ uiHelper.ShortToast("用户名不能为空!"); return false; }else if(pwd.length()<1){ uiHelper.ShortToast("密码不能为空!"); return false; } return true; } private Handler loginHandler=new Handler(){ public void handleMessage(Message msg){ pbLogin.setVisibility(View.GONE); //将登录结果提示给用户 if(msg.what==100){ String info=""; String flag=(String)msg.obj; if(flag.equals("1")){ //info="登录成功!"; USER_NAME=name; Intent intent=new Intent(MainActivity.this,OtherActivity.class); startActivity(intent); finish(); return; }else if(flag.equals("0")){ info="用户名或密码错误!"; }else{ info="登录失败请稍后重试!"; } uiHelper.ShortToast(info); } } }; class LoginThread implements Runnable{ public void run(){ String url="http://192.168.0.116/android/login.php?name="+name+"&pwd="+pwd; String flag=NetHelper.doLogin(url); if(flag.equals("1")){ //记住用户登录信息 saveSharedPreferences(); } loginHandler.obtainMessage(100, flag).sendToTarget(); } } }
(5)OtherActivity.java 主要用于用户登录成功后信息的显示
public class OtherActivity extends Activity { private TextView tvShow=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); tvShow=(TextView)findViewById(R.id.tv_show); tvShow.setText(MainActivity.USER_NAME); } }
3:最后不要忘了在AndroidManifest.xml文件中添加:
<uses-permission android:name="android.permission.INTERNET" />