作为一名android开发者,通过博客记录自己成长的道路,以下是小白在开发实践中的登录功能实现的一些步骤及功能,希望对做这一部分功能的伙伴有一定的帮助

1.登录的界面布局

2.登录的url、sp保存token、okhttp post请求

3.登录的ui界面,功能代码....

登录界面如下,勿喷

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"><de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/ccimgv_head"
        android:layout_width="62dp"
        android:layout_height="62dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="95dp"
        android:src="@mipmap/ic_launcher"
        app:civ_border_color="#0DC5E6"
        app:civ_border_width="2dp"/><TextView
        android:id="@+id/tv_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="12dp"
        android:text="用户名"
        android:textColor="#323232"
        android:textSize="15sp"/><TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="38dp"
        android:text="手机号码"
        android:textColor="#2D2D2D"
        android:textSize="10sp"/><LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="37dp"
        android:layout_marginRight="37dp"
        android:layout_marginTop="4dp"
        android:background="#FFFFFF"
        android:orientation="horizontal"><ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:src="@mipmap/registphone"/><EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="13dp"
            android:background="@null"
            android:hint="123456789"
            android:textSize="12sp"/></LinearLayout><TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="51dp"
        android:layout_marginTop="6dp"
        android:text="密码"
        android:textColor="#2D2D2D"
        android:textSize="10sp"/><LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="37dp"
        android:layout_marginRight="37dp"
        android:layout_marginTop="4dp"
        android:background="#FFFFFF"><ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="8dp"
            android:src="@mipmap/password"/><EditText
            android:id="@+id/et_psw"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="13dp"
            android:background="@null"
            android:hint="密码"
            android:textSize="12sp"/></LinearLayout><!-- 登录按钮 -->
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="37dp"
        android:layout_marginRight="37dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/shape_login_bg"
        android:text="@string/login"
        android:textColor="#FDFDFD"
        android:textSize="14sp"/><LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginLeft="37dp"
        android:layout_marginRight="37dp"
        android:layout_marginTop="17dp"><Button
            android:id="@+id/btn_user_register"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="用户注册"
            android:textColor="#9F9F9F"
            android:textSize="12sp"/><View
            android:layout_width="1dp"
            android:layout_height="14dp"
            android:layout_gravity="center_horizontal"
            android:background="#DCDCDC"/><Button
            android:id="@+id/btn_forget_psw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:text="忘记密码"
            android:textColor="#9F9F9F"
            android:textSize="12sp"/></LinearLayout><View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/background"/></LinearLayout>

okhttp post请求登录

LoginActivity

package fho.nak.jeekup.nakfho.ui.loginandregister;import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import com.google.gson.Gson;import org.json.JSONException;
import org.json.JSONObject;import java.io.IOException;import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import fho.nak.jeekup.nakfho.MainActivity;
import fho.nak.jeekup.nakfho.R;
import fho.nak.jeekup.nakfho.base.BaseActivity;
import fho.nak.jeekup.nakfho.bean.ResultData;
import fho.nak.jeekup.nakfho.utils.MD5Utils;
import fho.nak.jeekup.nakfho.utils.PreferenceUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;/**
 * @创建者 Jimven
 * @创建时间 2017/7/21/0021  11:35
 */

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity {String loginUrl    = "http://www.xxx";@Bind(R.id.ccimgv_head)CircleImageView mCcimgvHead;@Bind(R.id.tv_username)TextView        mTvUsername;@Bind(R.id.et_phone)EditText        mEtPhone;@Bind(R.id.et_psw)EditText        mEtPsw;@Bind(R.id.btn_login)Button          mBtnLogin;@Bind(R.id.btn_user_register)Button          mBtnUserRegister;@Bind(R.id.btn_forget_psw)Button          mBtnForgetPsw;private MediaType mMediaType;private String mToken;@Override
    protected int setLayoutId() {return R.layout.activity_login;}@Override
    protected void setUpView() {initView();initData();initEvent();}//初始化组件
    private void initView() {}//初始化数据
    private void initData() {}//初始化事件
    private void initEvent() {}@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// TODO: add setContentView(...) invocation
        ButterKnife.bind(this);}@OnClick({  R.id.btn_login, R.id.btn_user_register, R.id.btn_forget_psw})public void onClick(View view) {switch (view.getId()) {case R.id.btn_login://登录操作
                login();break;case R.id.btn_user_register:startActivity(new Intent(this, RegisterActivity.class));break;case R.id.btn_forget_psw:startActivity(new Intent(this, ResetpswActivity.class));break;}}//登录操作
    private void login() {final String phone = mEtPhone.getText().toString();final String userPsw = mEtPsw.getText().toString();final String md5Password = MD5Utils.md5Password(userPsw);//加密MD5
        //判断是否非空
        if (!TextUtils.isEmpty(temphone) && !TextUtils.isEmpty(tempsw)) {//都不为空
            //请求数据

            OkHttpClient client = new OkHttpClient();final JSONObject jsonObject = new JSONObject();try {//提交的参数jsonObject.put("loginName", phone);
jsonObject.put("password", md5Password);} catch (JSONException e) {e.printStackTrace();}mMediaType = MediaType.parse("application/json; charset=utf-8");final RequestBody requestBody = RequestBody.create(mMediaType, jsonObject.toString());Request request = new Request.Builder().post(requestBody).url(LoginUrl).build();Call call = client.newCall(request);call.enqueue(new Callback() {@Override
                public void onFailure(Call call, IOException e) {//请求失败
                    Log.i("请求情况:", "请求失败");}@Override
                public void onResponse(Call call, Response response) throws IOException {if (response.isSuccessful()) {Log.i("响应状态", "响应成功");String loginBody = response.body().string();Gson gson = new Gson();ResultData loginData = gson.fromJson(loginBody, ResultData.class);String loginResultCode = loginData.getResultCode();Log.i("返回状态码", loginResultCode);//响应成功,判断状态码
                        if (loginResultCode.equals("100")) {Log.i("登录状态", "登录成功");//获取tokenxz
                            Object data = loginData.getData();String data1 = data.toString();JSONObject jsonObject1 = null;try {jsonObject1 = new JSONObject(data1);mToken = jsonObject1.optString("token");Log.i("打印token", mToken);} catch (JSONException e) {e.printStackTrace();}                  
                            //登录成功,跳到主界面                            startActivity(new Intent(LoginActivity.this, MainActivity.class));                            finish();                        } else if (loginResultCode.equals("500")) {                            Log.i("登录状态", "登录失败");                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();                        } else {                            Log.i("Jimven", "其他");                        }                    } else {                        Log.i("响应情况:", "响应失败");                    }                }            });        } else {            Toast.makeText(LoginActivity.this, "请填写完整的信息", Toast.LENGTH_SHORT).show();        }    }}
            //保存token
         //用sp工具保存
           PreferenceUtils.putString(getApplicationContext(), "token", mToken);

附上sp工具代码

PreferenceUtils.java

package fho.nak.jeekup.nakfho.utils;import android.content.Context;
import android.content.SharedPreferences;/**
 * sp工具类
 */
public class PreferenceUtils {public static void putString(Context context, String key, String value){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);sp.edit().putString(key, value).commit();}public static String getString(Context context, String key, String defValue){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);return sp.getString(key, defValue);}public static String getString(Context context, String key){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);return getString(context,key,"");}public static void putBoolean(Context context, String key, boolean value){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);sp.edit().putBoolean(key, value).commit();}public static boolean getBoolean(Context context, String key, boolean defValue){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);return sp.getBoolean(key, defValue);}public static boolean getBoolean(Context context, String key){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);return getBoolean(context,key,false);}public static void putInt(Context context, String key, int value){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);sp.edit().putInt(key, value).commit();}public static int getInt(Context context, String key, int defValue){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);return sp.getInt(key, defValue);}public static int getInt(Context context, String key){SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);return getInt(context,key,-1);}}

以上简单的登录功能就完成了,希望对大家有帮助,也希望大家能够多多交流,一起成长

okhttp post提交参数完成登录功能 保存返回的token相关推荐

  1. Spring Security 实战:登录成功后返回 JWT Token

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 170元买400元书的机会又来啦! 1. 前言 欢迎阅读 Spring Security 实 ...

  2. 简单登录功能(一)token的使用

    目录 1.token介绍 2.前端代码 3.后端代码 1.token介绍 前端使用的layui,通过ajax异步提交表单来实现登录,刚好学习巩固一下ajax,后端主要学习一下token的使用. tok ...

  3. H5-阿里云JSSDK集成、实现一键登录功能

    目录 一.阿里云H5端JSSDK集成文档地址 二.需要在H5页面中集成号码认证服务的JSSDK,并在服务端完成API对接 三.react项目HTML代码head标签中增加以下代码 四.初始化实例 五. ...

  4. Android逆向之路---Faceu的登录功能真的只提交了用户名和密码吗

    问题 几乎99%的软件都有登录功能,而登录这一个动作真的将我们的用户名和密码上传到了服务器吗,会不会有个人隐私呢.根据我们这个问题,我们用FaceU这个软件,逆向来看看他的登录功能到底都传了什么数据. ...

  5. vue登录如何存储cookie_vue项目实现表单登录页保存账号和密码到cookie功能

    实现功能: 1.一周内自动登录勾选时,将账号和密码保存到cookie,下次登陆自动显示到表单内 2.点击忘记密码则清空之前保存到cookie的值,下次登陆需要手动输入 次要的就不说了直接上主要的代码 ...

  6. 微信小程序-注册登录功能-本地数据保存-页面数据交替

    Title:微信小程序-注册登录功能-本地数据保存-页面数据交替 完美-小程序登录注册功能.rar-- 访问码:yqa5 1.主页面 主页面login.js代码 // pages/login/logi ...

  7. 一步步带你做vue后台管理框架(三)——登录功能

    系列教程<一步步带你做vue后台管理框架>第三课 github地址:vue-framework-wz 线上体验地址:立即体验 <一步步带你做vue后台管理框架>第一课:介绍框架 ...

  8. 01: 实现注册登录功能

    目录:抽屉项目之js最佳实践 01: 实现注册登录功能 02: 实现发布帖子功能 03: 将帖子展示到页面.点赞 04: 层级评论 目录: 1.1 显示.隐藏 "登录/注册" 菜单 ...

  9. day07-vue项目-搭建项目到登录功能

    文章目录 1.电商业务概述 2.项目初始化 3.码云相关操作 B.安装git D.在本地创建公钥:在终端运行:ssh-keygen -t rsa -C "xxx@xxx.com" ...

最新文章

  1. java泛型之有界类型
  2. oracle 唯一递增列,在oracle中创建unique唯一约束(单列和多列)
  3. Solr7 安装部署 管理界面介绍
  4. 信号归一化功率_UE低发射功率余量分析
  5. 零售连锁管理软件_连锁超市用哪个收银软件好?
  6. 静态属性,函数闭包,call/apply,继承
  7. 面试官 | 说一下什么是代理模式?
  8. Linux系统:centos7下搭建Nginx和FastDFS文件管理中间件
  9. Pandas知识点-索引和切片操作
  10. 一、Arcgis api js -- 基本概念
  11. iphone开发常用编码
  12. 你想要的宏基因组-微生物组知识全在这(2022.5)
  13. 微星主板黑苹果_黑苹果安装教程:准备磁盘+主板BIOS设置——墨涩网
  14. 豪越智慧后勤解决方案(教育/高校)
  15. Linux 下安装和配置 MinDoc
  16. Android Provision 的作用
  17. 链接脚本.lds(详细)总结附实例快速掌握
  18. PAT甲级 1016 Phone Bills(时间差)
  19. 5款高校5G消息应用号测评:覆盖校园服务的方方面面
  20. 中国AGR硅橡胶线行业产销情况与应用前景预测报告(新版)2022-2027

热门文章

  1. 解决argument list too long错误
  2. 基于facenet的实时人脸检测
  3. postman请求头加密_postman认证使用篇(五)
  4. C语言学习笔记——函数
  5. python数据分析基础009 -利用pandas带你玩转excel表格(下篇)
  6. 微信后台服务器地址验证的逻辑
  7. Torch dataloader 参数详解
  8. 被执行人与失信被执行人有何区别?
  9. 捡到iphone6怎么解锁_捡了一部苹果6,怎么解开指纹锁,
  10. Android 手机静态IP 连接局域网访问局域网电脑Web服务器