如何提高程序的健壮性,增加判断。
来源:互联网 发布:电脑软件连不上网络 编辑:程序博客网 时间:2024/05/02 04:35
如果后台在返回的时候少了一个属性?前端会不会报错,这是我们前端开发一定会遇到的问题。那么如何的提高程序的健壮性呢。
关于这个问题,我们可以在实例化这个对象的时候进行判读来增加程序的健壮性。下面是一个参考示例。
public class ClientUser implements Parcelable {
public static final Parcelable.Creator<ClientUser> CREATOR= new Parcelable.Creator<ClientUser>() {
public ClientUser createFromParcel(Parcel in) {
return new ClientUser(in);
}
public ClientUser[] newArray(int size) {
return new ClientUser[size];
}
};
/**用户注册V账号*/
private String userId;
/**用户昵称*/
private String userName = "";
/**用户注册Appkey*/
private String appKey;
/**用户注册Token*/
private String appToken;
private String password;
// 1男
private int sex;
private long birth;
private int pVersion;
private String signature;
private ECInitParams.LoginAuthType loginAuthType;
public int getpVersion() {
return pVersion;
}
public void setpVersion(int pVersion) {
this.pVersion = pVersion;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppToken() {
return appToken;
}
public void setAppToken(String appToken) {
this.appToken = appToken;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ECInitParams.LoginAuthType getLoginAuthType() {
return loginAuthType;
}
public void setLoginAuthType(ECInitParams.LoginAuthType loginAuthType) {
this.loginAuthType = loginAuthType;
}
public long getBirth() {
return birth;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public void setBirth(long birth) {
this.birth = birth;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public ClientUser(String userId) {
this.userId = userId;
}
private ClientUser(Parcel in) {
this.userId = in.readString();
this.userName = in.readString();
this.appKey = in.readString();
this.appToken = in.readString();
this.password = in.readString();
this.signature = in.readString();
this.sex = in.readInt();
this.birth = in.readLong();
this.pVersion = in.readInt();
this.loginAuthType = ECInitParams.LoginAuthType.fromId(in.readInt());
}
@Override
public String toString() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("userId" , userId);
jsonObject.put("userName" , userName);
jsonObject.put("appKey" , appKey);
jsonObject.put("appToken" , appToken);
jsonObject.put("inviteCode" , password);
jsonObject.put("sex" , sex);
jsonObject.put("personSign" , signature);
jsonObject.put("birth" , birth);
jsonObject.put("pVersion" , pVersion);
jsonObject.put("loginAuthType" , loginAuthType.getAuthTypeValue());
return jsonObject.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return "ClientUser{" +
"userId='" + userId + '\''+
", userName='" + userName + '\'' +
", appKey='" + appKey + '\'' +
", appToken='" + appToken + '\'' +
", inviteCode='" + password + '\'' +
'}';
}
public ClientUser from(String input) {
JSONObject object = null;
try {
object = new JSONObject(input);
if(object.has("userId")) {
this.userId = object.getString("userId");
}
if(object.has("userName")) {
this.userName = object.getString("userName");
}
if(object.has("appKey")) {
this.appKey = object.getString("appKey");
}
if(object.has("appToken")) {
this.appToken = object.getString("appToken");
}
if(object.has("inviteCode")) {
this.password = object.getString("inviteCode");
}
if(object.has("sex")) {
this.sex = object.getInt("sex");
}
if(object.has("birth")) {
this.birth = object.getLong("birth");
}
if(object.has("personSign")) {
this.signature = object.getString("personSign");
}
if(object.has("pVersion")) {
this.pVersion = object.getInt("pVersion");
}
if(object.has("loginAuthType")) {
this.loginAuthType = ECInitParams.LoginAuthType.fromId(object.getInt("loginAuthType"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return this;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.userId);
dest.writeString(this.userName);
dest.writeString(this.signature);
dest.writeString(this.appKey);
dest.writeString(this.appToken);
dest.writeString(this.password);
dest.writeInt(this.sex);
dest.writeLong(this.birth);
dest.writeInt(this.pVersion);
dest.writeInt(this.loginAuthType.getAuthTypeValue());
}
}
- 如何提高程序的健壮性,增加判断。
- 如何提高nodejs程序的稳定性,健壮性
- 提高程序健壮性的方法
- 如何提高代码的健壮性
- 如何提高代码的健壮性需求?
- 程序的健壮性
- 程序的健壮性
- 何谓程序的健壮性
- 程序的健壮性
- 程序的健壮性
- 【C++】使用const提高程序健壮性
- const使用提高程序健壮性总结
- 提高程序健壮性之assert使用
- 利用断言提高mybatis程序健壮性
- setsockopt()改善程序的健壮性
- setsockopt()改善程序的健壮性
- setsockopt()改善程序的健壮性
- setsockopt()改善程序的健壮性
- Learn Unity(0)——basic C# for Unity
- [LeetCode] Burst Balloons
- 剑指offer-面试题26:复杂链表的复制
- Eclipse下Maven工程多模块继承和聚合的创建
- 大数据工程师:大数据的java基础 第四周
- 如何提高程序的健壮性,增加判断。
- android studio 开发 Execution failed for task
- cubic-bezier贝塞尔曲线CSS3动画工具
- 简单对象访问协议
- ViewController的生命周期
- 剑指offer:滑动窗口的最大值
- 1134: 【C语言训练】求PI*
- 拼图响应式前端框架版响应式后台正式发布
- c语言中static 函数和普通函数的区别?