安卓原生开发之登录功能实现

思路

1、 给app分配网络访问的权限
2、 启动页面(LoginActivity)为登录页面
3、 Helloword页面(MainActivity)为登录成功的跳转页面
4、 输入的账号密码正确则跳转,否则不跳转
5、 使用Intent实现页面的跳转
6、 使用httpclient发送登录的异步请求
7、 AndroidManifest配置文件里面添加登录页面的activity
8、 开发登录接口,对app客户端发送的登录请求进行处理
参考资料:
https://blog.csdn.net/jiayu0855/article/details/78759471

开发环境搭建

安卓原生开发之登录功能实现_第1张图片
image.png

参考资料:
http://www.runoob.com/w3cnote/android-tutorial-android-studio.html

添加httpclient框架和fastjson框架的依赖

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'cz.msebera.android:httpclient:4.4.1.1'
    compile 'org.apache.httpcomponents:httpcore:4.4.2'
    compile 'com.alibaba:fastjson:1.2.21'
}

helloword页面设计

Java代码如下:

package com.yangzc.myerp;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

xml配置如下:



    


登录页面设计

Java代码如下:

package com.yangzc.myerp;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;


public class LoginActivity extends AppCompatActivity {
    private EditText account;
    private EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //获取用户名的id
        account = findViewById(R.id.uname);
        //获取密码的id
        password = findViewById(R.id.upass);
    }

    public void LoginAsyncHttpClient(View view){
        //获取用户名的值
        String name=account.getText().toString();
        //获取密码的值
        String pass=password.getText().toString();
        //获取网络上的servlet路径
        String path="http://192.168.1.14:8080/myerp/login.action";
        //使用第三方
        AsyncHttpClient ahc=new AsyncHttpClient();
        //请求参数
        RequestParams params=new RequestParams();
        //给请求参数设键和值(键的名字和web后台保持一致)
        params.put("name",name);
        params.put("pwd",pass);
        //设值提交方式
        ahc.post(this,path,params,new TextHttpResponseHandler(){
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) {
                //super.onFailure(statusCode, headers, responseBody, error);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseBody) {
                System.out.println("服务器返回的内容为:"+responseBody);
                JSONObject obj = (JSONObject)JSON.parse(responseBody);
                String result = obj.getString("result");
                //吐司Android studio与web后台数据交互获得的值
                Toast.makeText(LoginActivity.this, obj.getString("message"), Toast.LENGTH_SHORT).show();
                if(result.equals("success")){
                    System.out.println("跳转页面");
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    //关闭当前界面
                    finish();
                }
            }
        });
    }
}

xml配置如下:



    
    
    

修改AndroidManifest配置

给原生app分配网络访问的权限


登录页面的activity配置


    
        
        
    

Helloworld页面的activity配置



开发登录接口

后台代码如下:

package com.myerp.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.myerp.dao.UserDAO;


public class ActionServlet2 extends HttpServlet {
    
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //1.获取URI
        String uri = request.getRequestURI();
        HttpSession session = request.getSession(); 
        //2.截取URI中的动作
        uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
        if(uri.equals("/login")){
            //判断用户名和密码
            String uname = request.getParameter("name");
            String pwd = request.getParameter("pwd");
            UserDAO udao = new UserDAO(); 
            try{
                //登录认证
                if(udao.auth(uname,pwd)==false){
                    //登录失败
                    //request.setAttribute("msg","用户名或密码不正确");
                    response.getWriter().print("{\"result\":\"fail\",\"message\":\"username or password is wrong\"}");
                }else{
                    //登录成功,记录用户信息到session中
                    session.setAttribute("uname",uname);
                    //session过期时间设置为5分钟
                    session.setMaxInactiveInterval(300);
                    response.getWriter().print("{\"result\":\"success\",\"message\":\"login success\"}");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

测试

安卓原生开发之登录功能实现_第2张图片
image.png

安卓原生开发之登录功能实现_第3张图片
image.png

安卓原生开发之登录功能实现_第4张图片
image.png

你可能感兴趣的:(安卓原生开发之登录功能实现)