1、在build.gradle导入okHttp3
2、activity_mian.xml样式文件
3、创建保存账号密码类
4、主页代码
5、完成以上4个步骤后,部分手机上用不了(如果您使用的是http,AndroID9.0手机是用不了的,看本博客的“Android P不能使用http”解决不能使用http的方法)。
在build.gradle导入okhttp3(加入进去后记得在AndroID studio的右上角点击“Sync Now”同步)
implementation ‘com.squareup.okhttp3:okhttp:3.4.1‘ //okhttp3
activity_mian.xml样式文件
<?xml version="1.0" enCoding="utf-8"?><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" tools:context=".MainActivity"> <ImageVIEw androID:ID="@+ID/iv" androID:layout_wIDth="70dp" androID:layout_height="70dp" androID:layout_centerHorizontal="true" androID:layout_margintop="40dp" androID:background="@drawable/dongman"/> <linearLayout androID:ID="@+ID/ll_number" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/iv" androID:layout_centerVertical="true" androID:layout_margintop="15dp" androID:layout_marginleft="10dp" androID:layout_marginRight="10dp" androID:layout_marginBottom="5dp" androID:background="#ffffff"> <TextVIEw androID:ID="@+ID/tv_number" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="账号" androID:textcolor="#000" androID:textSize="20sp"/> <EditText androID:ID="@+ID/et_number" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginleft="5dp" androID:background="@null" androID:padding="10dp"/> </linearLayout> <linearLayout androID:ID="@+ID/ll_password" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/ll_number" androID:layout_centerVertical="true" androID:layout_margintop="15dp" androID:layout_marginleft="10dp" androID:layout_marginRight="10dp" androID:layout_marginBottom="5dp" androID:background="#ffffff"> <TextVIEw androID:ID="@+ID/tv_password" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="密码" androID:textcolor="#000" androID:textSize="20sp"/> <EditText androID:ID="@+ID/et_password" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginleft="5dp" androID:background="@null" androID:padding="10dp"/> </linearLayout> <button androID:ID="@+ID/btn_login" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_below="@+ID/ll_password" androID:layout_marginleft="10dp" androID:layout_marginRight="10dp" androID:layout_margintop="30dp" androID:text="登录" androID:background="#3C8DC4" androID:textSize="20sp"/></relativeLayout>
创建保存账号密码类
package com.example.remembernp;import androID.content.Context;import java.io.fileinputStream;import java.io.fileOutputStream;import java.util.HashMap;import java.util.Map;public class Savefile { //把账号密码保存在data.txt文件中 public static boolean saveUserInfo(Context context,String number,String password){ try{ fileOutputStream fos = context.openfileOutput("data.txt",Context.MODE_PRIVATE); fos.write((number + ":" + password).getBytes()); fos.close(); return true; }catch (Exception e){ e.printstacktrace(); return false; } } //从data.txt中去获取刚刚保存的账号密码 public static Map<String,String> getUserInfo(Context context) { String content = ""; try { fileinputStream fis = context.openfileinput("data.txt"); byte[] buffer = new byte[fis.available()]; fis.read(buffer);//读取 content = new String(buffer); Map<String,String > userMap = new HashMap<String,String>(); String[] infos = content.split(":"); userMap.put("number",infos[0]); userMap.put("password",infos[1]); fis.close(); return userMap; }catch (Exception e){ e.printstacktrace(); return null; } }}
主页代码
package com.example.remembernp;import androIDx.appcompat.app.AppCompatActivity;import androID.app.AlertDialog;import androID.icu.util.LocaleData;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.Toast;import org.Json.JsONException;import org.Json.JsONObject;import java.io.IOException;import java.io.UnsupportedEnCodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Map;import okhttp3.Call;import okhttp3.Callback;import okhttp3.MediaType;import okhttp3.OkhttpClIEnt;import okhttp3.Request;import okhttp3.Requestbody;import okhttp3.Response;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{ private EditText etNumber; private EditText etPassword; private button btnLogin; private MediaType mediaType; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button btn = (button)findVIEwByID(R.ID.btn_login); Map<String,String> userInfo = Savefile.getUserInfo(this); initVIEw(); if(userInfo != null){ etNumber.setText(userInfo.get("number")); etPassword.setText(userInfo.get("password")); } } private voID initVIEw(){ etNumber = (EditText)findVIEwByID(R.ID.et_number); etPassword = (EditText)findVIEwByID(R.ID.et_password); btnLogin = (button)findVIEwByID(R.ID.btn_login); btnLogin.setonClickListener(this); } @OverrIDe public voID onClick(VIEw vIEw) { //单击事件,获取账号密码 final String number = etNumber.getText().toString().trim(); String password = etPassword.getText().toString().trim(); //检查账号密码是否正确 if(TextUtils.isEmpty(number)){ Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(password)){ Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show(); return; } okhttp(); //登录 } private voID okhttp(){ //单击事件,获取账号密码 final String number = etNumber.getText().toString().trim(); final String password = etPassword.getText().toString().trim(); //给密码加密 final String md5 = md5Decode(password + "[email protected]#[email protected]#$#@KJdjklj;D../dSF.,"); new Thread(new Runnable() { @OverrIDe public voID run() { try { OkhttpClIEnt clIEnt = new OkhttpClIEnt(); JsONObject JsonObject = new JsONObject(); JsonObject.put("number",number); JsonObject.put("password",md5); mediaType = MediaType.parse("application/Json;charest=utf-8"); Requestbody requestbody = Requestbody.create(mediaType,JsonObject.toString()); final Request request = new Request.Builder() .url("使用自己的登录网址") .post(requestbody).build(); clIEnt.newCall(request).enqueue(new Callback() { @OverrIDe //请求失败 public voID onFailure(Call call,IOException e) { Log.d("请求失败",",返回码:"+e.getMessage()); loginRemind(400); } @OverrIDe //请求成功 public voID onResponse(Call call,final Response response) throws IOException { loginRemind(response.code()); if(response.code() == 200){ Log.d("请求成功---------","后台返回数据:"+response.body().string()); //保存账号密码 Savefile.saveUserInfo(MainActivity.this,number,password); }else{ Log.d("请求成功-----但登录失败----返回码:"+response.code(),"------失败原因:"+response.body().string()); } } }); } catch (JsONException e) { e.printstacktrace(); } } }).start(); } public String md5Decode(String content) { byte[] hash; try { hash = MessageDigest.getInstance("MD5").digest(content.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("NoSuchAlgorithmException",e); } catch (UnsupportedEnCodingException e) { throw new RuntimeException("UnsupportedEnCodingException",e); } //对生成的16字节数组进行补零 *** 作 StringBuilder hex = new StringBuilder(hash.length * 2); for (byte b : hash) { if ((b & 0xFF) < 0x10) { hex.append("0"); } hex.append(Integer.toHexString(b & 0xFF)); } return hex.toString(); } private voID loginRemind(final int num){ MainActivity.this.runOnUiThread(new Runnable() { @OverrIDe public voID run() { switch (num){ case 200: Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show(); break; case 400: Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_LONG).show(); break; default: Toast.makeText(MainActivity.this,"请求成功,登录失败,返回码:"+num,Toast.LENGTH_LONG).show(); break; } } }); }}@H_403_53@ 总结
以上是内存溢出为你收集整理的安卓原生登录(记住账号密码、获取后台数据)全部内容,希望文章能够帮你解决安卓原生登录(记住账号密码、获取后台数据)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)