最近,需要在项目中引用jackson开发库,为什么要引入这个库,不能使用Gson吗?可以使用Gson,但是为了使用jackson的忽略实体类字段的功能,而我不知道Gson该如何设置忽略,这是前提。
需要引入的jackson文件:
运行时报错:
Error:Execution failed for task ‘:app:transformResourcesWithMergeJavaResForDebug’.
com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
File1: D:\work\studiopro\SevenCalendar\app\libs\jackson-annotations-2.7.5.jar
File2: D:\work\studiopro\SevenCalendar\app\libs\jackson-core-2.7.5.jar
File3: D:\work\studiopro\SevenCalendar\app\libs\jackson-databind-2.7.5.jar
截图如下:
报错看来是因为三个jar中包含了同样的LICENSE.txt文件,这样打包的时候有可能会相互覆盖,studio检测到这种情况,就抛出错误。
解决办法:
在app的build.gradle中增加配置:
packagingOptions {
exclude ‘META-INF/LICENSE’
exclude ‘META-INF/NOTICE’
}
添加配置之后的build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.seven.sevencalendar"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.3.1'
compile project(':calendar')
compile files('libs/jackson-core-2.7.5.jar')
compile files('libs/jackson-annotations-2.7.5.jar')
compile files('libs/jackson-databind-2.7.5.jar')
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.zhy:okhttputils:2.6.2'
}
ok,报错问题到此解决。
备注:
为什么要设置忽略呢?答案是为了避免后台返回的字段跟前端定义的不一致(有可能是后台添加或者减少字段而没有通知前端),而导致前端解析数据抛出异常的情况。
使用jackson设置忽略很简单,一行代码足矣。
package com.seven.sevencalendar.bean;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
//就是下面这一行代码**************
@JsonIgnoreProperties(ignoreUnknown = true)
public class TimeSchedule implements Serializable {
private static final long serialVersionUID = 1L;
//2017.10.11新增字段
String threeState;
String overdueDay;
public String getThreeState() {
return threeState;
}
public void setThreeState(String threeState) {
this.threeState = threeState;
}
public String getOverdueDay() {
return overdueDay;
}
public void setOverdueDay(String overdueDay) {
this.overdueDay = overdueDay;
}
/**
* 待办事项ID
*/
String _id;
/**
* 经销商代码
*/
String delearCode;
/**
* 销售顾问代码
*/
String scCode;
/**
* 待办分类 ( '店内事务','完善资料','联系客户','预约接待','外出')
*/
String scheduleHead;
/**
* 待办子类(因公外出 因私外出 店内市场活动 推送预约到店 自建线索预约 交车 未到店邀约 已到店跟进)
*/
String scheduleSub;
/**
* 待办小类 (休假 外展 外拓 其他)
*/
String scheduleSmall;
/**
* 开始时间(2016/08/16 06:55:34)
*/
String startTime;
/**
* 结束时间
*/
String endTime;
/**
* 内容
*/
String content;
/**
* 备注
*/
String remark;
/**
* 属性类型(活动 顾客 线索)
*/
String attrType;
/**
* 属性id
*/
String attrNum;
/**
* 更新人
*/
String user;
/**
* 顾客性别
*/
String custSex;
/**
* 顾客姓名
*/
String custName;
/**
* 上次进店日期
*/
String custLastInroomDate;
/**
* 顾客级别
*
*/
String custLevel;
/**
* 顾客意向车型
*/
String custCar;
/**
* 顾客状态
*/
String custState;
/**
* 顾客联系电话
*/
String custPhone;
public TimeSchedule() {
super();
}
public TimeSchedule(String delearCode, String scCode, String scheduleHead,
String scheduleSub, String scheduleSmall, String startTime,
String endTime, String content, String remark, String attrType,
String attrNum, String user) {
super();
this.delearCode = delearCode;
this.scCode = scCode;
this.scheduleHead = scheduleHead;
this.scheduleSub = scheduleSub;
this.scheduleSmall = scheduleSmall;
this.startTime = startTime;
this.endTime = endTime;
this.content = content;
this.remark = remark;
this.attrType = attrType;
this.attrNum = attrNum;
this.user = user;
}
/**
* 获取日历时使用
*/
public TimeSchedule(String id, String delearCode, String scCode,
String scheduleHead, String scheduleSub, String scheduleSmall,
String startTime, String endTime, String content, String remark,
String attrType, String attrNum, String user) {
super();
this._id = id;
this.delearCode = delearCode;
this.scCode = scCode;
this.scheduleHead = scheduleHead;
this.scheduleSub = scheduleSub;
this.scheduleSmall = scheduleSmall;
this.startTime = startTime;
this.endTime = endTime;
this.content = content;
this.remark = remark;
this.attrType = attrType;
this.attrNum = attrNum;
this.user = user;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSex() {
return custSex;
}
public void setCustSex(String custSex) {
this.custSex = custSex;
}
public String getCustLastInroomDate() {
return custLastInroomDate;
}
public void setCustLastInroomDate(String custLastInroomDate) {
this.custLastInroomDate = custLastInroomDate;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustCar() {
return custCar;
}
public void setCustCar(String custCar) {
this.custCar = custCar;
}
public String getCustState() {
return custState;
}
public void setCustState(String custState) {
this.custState = custState;
}
public String getId() {
return _id;
}
public void setId(String id) {
this._id = id;
}
public String getDelearCode() {
return delearCode;
}
public void setDelearCode(String delearCode) {
this.delearCode = delearCode;
}
public String getScCode() {
return scCode;
}
public void setScCode(String scCode) {
this.scCode = scCode;
}
public String getScheduleHead() {
return scheduleHead;
}
public void setScheduleHead(String scheduleHead) {
this.scheduleHead = scheduleHead;
}
public String getScheduleSub() {
return scheduleSub;
}
public void setScheduleSub(String scheduleSub) {
this.scheduleSub = scheduleSub;
}
public String getScheduleSmall() {
return scheduleSmall;
}
public void setScheduleSmall(String scheduleSmall) {
this.scheduleSmall = scheduleSmall;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getAttrType() {
return attrType;
}
public void setAttrType(String attrType) {
this.attrType = attrType;
}
public String getAttrNum() {
return attrNum;
}
public void setAttrNum(String attrNum) {
this.attrNum = attrNum;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return String
.format("TimeSchedule [id=%s, delearCode=%s, scCode=%s, scheduleHead=%s, scheduleSub=%s, scheduleSmall=%s, startTime=%s, endTime=%s, content=%s, remark=%s, attrType=%s, attrNum=%s, user=%s, custSex=%s, custName=%s, custLastInroomDate=%s, custLevel=%s, custCar=%s, custState=%s, custPhone=%s]",
_id, delearCode, scCode, scheduleHead, scheduleSub,
scheduleSmall, startTime, endTime, content, remark,
attrType, attrNum, user, custSex, custName,
custLastInroomDate, custLevel, custCar, custState,
custPhone);
}
}