改造上传期间的日期字段更改

时间:2018-10-23 13:31:04

标签: java android retrofit2 interceptor okhttp

我正在使用Retrofit2上载SOAP请求。日期在上传过程中自动更改,每1000个请求中有10个。我已经进行了很多调查,并在我开始使用拦截器记录呼叫时查明了问题所在。

Log Printed before interceptor
fromTime:2018-10-13T13:22:00
isDeleted:false
name:Unknown visitor
reason:Unknown reason
toTime:2018-10-13T23:59:59
vrm:ABC


Log Printed in Interceptor.
FromTime:2018-10-20T11:59:00
Name:Unknown visitor
Reason:Unknown reason
ToTime:2018-10-13T23:59:59
Vrm:ABC
isDeleted:false

从时间:2018-10-13T13:22:00更改为从时间:2018-10-20T11:59:00可以看到。

构建请求如下

public EnvelopeSender build() {
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new EnvelopInterceptor()).build();
        Retrofit retrofit = new Retrofit.Builder().client(client)
                .baseUrl(url)
                .addConverterFactory(SimpleXmlConverterFactory.create(serializer))
                .addCallAdapterFactory(SynchronousCallAdapterFactory.create())
                .build();
        return retrofit.create(EnvelopeSender.class);
    }

序列化工厂如下。

公共类XMLSerializerFactory {

private Strategy strategy;
private DateFormat format;
private RegistryMatcher registryMatcher;

public Serializer getSerializer() {
    if (strategy == null && registryMatcher == null) {
        return new Persister();
    } else if (strategy == null) {
        return new Persister(registryMatcher);
    } else if (registryMatcher == null) {
        return new Persister(strategy);
    } else {
        return new Persister(strategy, registryMatcher);
    }
}

public XMLSerializerFactory withStrategy() {
    strategy = new AnnotationStrategy();
    return this;
}

public XMLSerializerFactory withDateFormat() {
    format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.UK);
    return this;
}

public XMLSerializerFactory withRegistryMatcher() {
    if (format == null)
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.UK);

    registryMatcher = new RegistryMatcher();
    registryMatcher.bind(Date.class, new DateFormatTransformer(format));
    return this;
}

}

public class DateFormatTransformer implements Transform<Date> {
    private DateFormat dateFormat;

    public DateFormatTransformer(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    @Override
    public Date read(String value) throws Exception {
        return dateFormat.parse(value);
    }

    @Override
    public String write(Date value) throws Exception {
        return dateFormat.format(value);
    }

}

是错误还是我做错了什么?

谢谢。

0 个答案:

没有答案