之前写过一个java web端的登录验证,最后返回一个json字符串。
字符串格式如下:
{"appmsg":"账号或密码错误","appcode":0,"_default_boolean_a":false}
今天就结合着Android来写一个简单的登录。
注意: 在AndroidManifest.xml里给访问网络的权限
在写具体路径时,一定要注意不要用localhost,因为识别不了。一定要写具体的IP
步骤如下:
1、先做好页面布局
2、在activity中获取用户名、密码
3、把用户名、密码附加到请求的地址上
4、new 一个子线程,在子线程里通过HttpURLConnection 去请求数据
5、根据连接返回的状态码判断是否成功连接
6、成功连接的话,就获取response的数据,返回inputstream流
7、将流转为String类型,通过UI线程的消息循环机制,将该String类型的结果返回到UI线程中
8、在UI线程中处理子线程中返回的结果
9、将String的结果转为JSONObject对象,去获取该对象中的code结果,0为登录失败,1为登录成功。
10、弹出Toast,提示用户登录失败或成功。
一、布局页面
首先是一个页面布局,如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/p_w_picpathView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:srcCompat="@drawable/qq"/>
<EditText
android:id="@+id/et_qqNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入QQ号码"
android:inputType="textPersonName"/>
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="输入密码"
android:inputType="textPassword"
/>
<CheckBox
android:id="@+id/cb_rember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="记住密码"
/>
<Button
android:id="@+id/bt_sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="login"
android:text="提交"/>
</LinearLayout>
2、MainActivity
package com.yuanlp.qqloginweb;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final int LOAD_SUCCESS =1 ;
private static final int LOAD_ERROR =2 ;
private EditText mQqNum;
private EditText mQqPwd;
private CheckBox mCb_rember;
/**
*主线程中建一个handler,来获取子线程中的Message
*
*/
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case LOAD_SUCCESS:
boolean flag=checkLogin(msg.obj.toString());
if (flag){
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
}
sub.setEnabled(true);
}
}
};
private Button sub;
private String mQq;
private String mPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQqNum= (EditText) findViewById(R.id.et_qqNum);
mQqPwd = (EditText) findViewById(R.id.et_pwd);
mCb_rember = (CheckBox) findViewById(R.id.cb_rember);
sub = (Button) findViewById(R.id.bt_sub);
}
public void login(View view){
//Toast.makeText(this,"点击了提交",Toast.LENGTH_SHORT).show();
mQq = mQqNum.getText().toString().trim();
mPwd = mQqPwd.getText().toString().trim();
//mCb_rember.getText().toString().trim();
if (TextUtils.isEmpty(mQq)||TextUtils.isEmpty(mPwd)){
Toast.makeText(this,"QQ号码或者密码为空",Toast.LENGTH_SHORT).show();
return;
}else {
//选中了保存密码
if (mCb_rember.isChecked()){
}
}
//这里设置按钮不能点,应为一直点,就一直发送请求,会造成一直请求数据
sub.setEnabled(false);
/**
* 点击按钮事件,在主线程中开启一个子线程进行网络请求
* (因为在4.0只有不支持主线程进行网络请求,所以一般情况下,建议另开启子线程进行网络请求等耗时操作)。
*/
//请求网络
new Thread(){
@Override
public void run() {
try {
Thread.sleep(5000);
String path="http://192.168.1.111:10010/aos/pdaLogin.jhtml?username="+ mQq +"&password="+ mPwd;
URL url = new URL(path);
//打开httpurlconnection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); //设置get方式获取数据
conn.setConnectTimeout(5000); //设置连接超时时间5秒
int code = conn.getResponseCode(); // 获取response状态,200表示成功获取资源,404表示资源不存在
if (code==200){
InputStream is=conn.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuffer sb=new StringBuffer();
String len=null;
while((len=br.readLine())!=null){
sb.append(len);
}
String result=sb.toString();
/**
* 子线程发送消息到主线程,并将获取的结果带到主线程,让主线程来更新UI。
*/
Message msg= Message.obtain();
msg.what=LOAD_SUCCESS;
msg.obj=result;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg=Message.obtain();
msg.what=LOAD_ERROR;
handler.sendMessage(msg);
}
}
}.start();
}
private boolean checkLogin(String result) {
boolean flag=false;
JSONObject jsonObject=null;
try {
jsonObject=new JSONObject(result);
String code=jsonObject.get("appcode").toString();
if ("0".equals(code)){
flag=false;
}else if("1".equals(code)){
flag=true;
}
} catch (JSONException e) {
e.printStackTrace();
}
return flag;
}
}
转载于:https://blog.51cto.com/cm0425/1941109