概述
step1: 加密和解密
package com.example.textdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String firstString = "eyJ3ZWJzaXQiOiJodHRwczpcL1wvbWcubWluZG9yZHouY29tXC9taW5kb3JcL2Rvd25sb2FkX2FwcGx5P2lkPTk2IiwiZGV2S2V5IjoiZTYyYzUwNmVkOWFhNDZjMmFiNTBlMDMzOTgzNWVmY2YiLCJkZXZTZWNyZXQiOiI5YTliMTZkNjg4MTc0YTIzYTAzMjY5NDliMjNlNTFjYiIsInVzZXJJZCI6Im1pbkFwcDExMzk4OCIsInBsYXRmb3JtIjoiaU9TIiwibW9kZWwiOiJpUGhvbmUiLCJlcXVpcG1lbnRzIjoiW3tcImVxdWlwbWVudElkXCI6XCJ6Y3owMDIxMDM5MTBcIixcImVxdWlwbWVudE5vdGVcIjpcIuS4gOawp+WMlueisyhjbylcIixcInByb2R1Y3RJZFwiOlwiemN6MDAyXCJ9XSIsInR5cGUiOiJzaGFyZSIsImRldmljZXMiOiJ6Y3owMDIxMDM5MTAiLCJuaWNrTmFtZSI6IiIsImNyZWF0ZVRpbWUiOjE2NTA1ODk3Nzd9";
/**
解析base64 字符串
* */
String lastString = base64Decode(firstString, "UTF-8");
Log.e("TAG", "" + lastString);
Gson gson = new Gson();
ScanCodeBean mScanCodeBean = gson.fromJson(lastString, ScanCodeBean.class);
String webSitString = mScanCodeBean.getWebsit();
String equipments = mScanCodeBean.getEquipments();
Log.e("TAG", "" + webSitString + "t" + equipments);
/* 差不多完成版本
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson2 = gsonBuilder.create();
ScanCodeDeviceBean[] testCase = gson2.fromJson(equipments, ScanCodeDeviceBean[].class);
// ScanCodeDeviceBean testCase = gson2.fromJson(equipments, ScanCodeDeviceBean.class);
String equipmentNote = testCase[0].getEquipmentNote();
Log.e("TAG", "" +equipmentNote);*/
List<ScanCodeDeviceBean> scanCodeDeviceBeanList = gson.fromJson(equipments, new TypeToken<List<ScanCodeDeviceBean>>() {
}.getType());
Log.e("TAG", "" + scanCodeDeviceBeanList);
/* ScanCodeDeviceBean mScanCodeDeviceBean = gson.fromJson(equipments, ScanCodeDeviceBean.class);
String equipmentId = mScanCodeDeviceBean.getEquipmentId();
Log.e("TAG", "" + equipmentId + "t");*/
}
/**
* Base64加密字符串
*
* @param content -- 代加密字符串
* @param charsetName -- 字符串编码方式
* @return
*/
private String base64Encode(String content, String charsetName) {
if (TextUtils.isEmpty(charsetName)) {
charsetName = "UTF-8";
}
try {
byte[] contentByte = content.getBytes(charsetName);
return Base64.encodeToString(contentByte, Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
/**
* Base64解密字符串
*
* @param content -- 待解密字符串
* @param charsetName -- 字符串编码方式
* @return
*/
private String base64Decode(String content, String charsetName) {
if (TextUtils.isEmpty(charsetName)) {
charsetName = "UTF-8";
}
byte[] contentByte = Base64.decode(content, Base64.DEFAULT);
try {
return new String(contentByte, charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}
step2: 生成json对象
package com.example.textdemo;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**生成json 首先生成list*/
List<ScanCodeDeviceBean> scanCodeDeviceBeanList = new ArrayList<>();
ScanCodeDeviceBean mScanCodeDeviceBean = new ScanCodeDeviceBean();
mScanCodeDeviceBean.setEquipmentId("zcz002103910");
mScanCodeDeviceBean.setEquipmentNote("一氧化碳(co)");
mScanCodeDeviceBean.setProductId("zcz002");
scanCodeDeviceBeanList.add(mScanCodeDeviceBean);
Log.e("TAG", "" +scanCodeDeviceBeanList);
Gson gson = new Gson();
// List<WellSectionEntity> sectionlist = gson.fromJson(jsonTemp, new TypeToken<List<WellSectionEntity>>(){}.getType());
String age = gson.toJson(scanCodeDeviceBeanList);
Log.e("TAG", "" +age);
/**list转字符串*/
ScanCodeBean mScanCodeBean = new ScanCodeBean();
mScanCodeBean.setWebsit("https://mg.mindordz.com/mindor/download_apply?id=96");
mScanCodeBean.setDevKey("e62c506ed9aa46c2ab50e0339835efcf");
mScanCodeBean.setDevSecret("9a9b16d688174a23a0326949b23e51cb");
mScanCodeBean.setUserId("minApp113988");
mScanCodeBean.setPlatform("iOS");
mScanCodeBean.setModel("iPhone");
mScanCodeBean.setEquipments(age);
mScanCodeBean.setType("share");
mScanCodeBean.setDevices("zcz002103910");
mScanCodeBean.setNickName("");
mScanCodeBean.setCreateTime(1650589777);
String name = gson.toJson(mScanCodeBean);
Log.e("TAG", "" +name);
}
}
step3:
package com.example.textdemo;
public class ScanCodeBean{
/**
* websit : https://mg.mindordz.com/mindor/download_apply?id=96
* devKey : e62c506ed9aa46c2ab50e0339835efcf
* devSecret : 9a9b16d688174a23a0326949b23e51cb
* userId : minApp113988
* platform : iOS
* model : iPhone
* equipments : [{"equipmentId":"zcz002103910","equipmentNote":"一氧化碳(co)","productId":"zcz002"}]
* type : share
* devices : zcz002103910
* nickName :
* createTime : 1650589777
*/
private String websit;
private String devKey;
private String devSecret;
private String userId;
private String platform;
private String model;
private String equipments;
private String type;
private String devices;
private String nickName;
private int createTime;
public String getWebsit() {
return websit;
}
public void setWebsit(String websit) {
this.websit = websit;
}
public String getDevKey() {
return devKey;
}
public void setDevKey(String devKey) {
this.devKey = devKey;
}
public String getDevSecret() {
return devSecret;
}
public void setDevSecret(String devSecret) {
this.devSecret = devSecret;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getEquipments() {
return equipments;
}
public void setEquipments(String equipments) {
this.equipments = equipments;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDevices() {
return devices;
}
public void setDevices(String devices) {
this.devices = devices;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getCreateTime() {
return createTime;
}
public void setCreateTime(int createTime) {
this.createTime = createTime;
}
}
step4:
package com.example.textdemo;
public class ScanCodeDeviceBean {
/**
* equipmentId : zcz002103910
* equipmentNote : 一氧化碳(co)
* productId : zcz002
*/
private String equipmentId;
private String equipmentNote;
private String productId;
public String getEquipmentId() {
return equipmentId;
}
public void setEquipmentId(String equipmentId) {
this.equipmentId = equipmentId;
}
public String getEquipmentNote() {
return equipmentNote;
}
public void setEquipmentNote(String equipmentNote) {
this.equipmentNote = equipmentNote;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
}
end
最后
以上就是平常口红为你收集整理的base64加密解密和json处理的全部内容,希望文章能够帮你解决base64加密解密和json处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复