如何使用自定义转换器?
How to use custom converter?
我想解析时间来自
<monday>
<item>
<time>00:00:00</time>
</item>
...
</monday>
只要
我将物品定义为
@Root(strict = false)
private static class Item {
@Element(name = "time")
@Convert(TimeConverter.class)
private Long time;
}
我的转换器
public class TimeConverter implements org.simpleframework.xml.convert.Converter<Long> {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
@Override
public Long read(InputNode node) throws Exception {
try {
String value = node.getValue();
return df.parse(value).getTime();
} catch (Exception e) {
Log.e("mcheck", "read: ", e);
return 0L;
}
}
@Override
public void write(OutputNode node, Long value) throws Exception {
try {
String v = df.format(new Date(value));
node.setValue(v);
} catch (Exception e) {
Log.e("mcheck", "write: ", e);
}
}
}
然而,当我解析它时,我收到
retrofit.RetrofitError: java.lang.NumberFormatException: Invalid long: "00:00:00"
由于这个异常没有在转换器中的 try-catch 块中捕获,我假设解析器根本不访问转换器。
我的改装 1.9 调用
OkHttpClient httpClient = new OkHttpClient();
RestAdapter.Builder builder = new RestAdapter.Builder();
builder.setEndpoint(url);
builder.setLogLevel(RestAdapter.LogLevel.FULL);
builder.setConverter(new SimpleXMLConverter());
builder.setClient(new OkClient(httpClient));
RestAdapter restAdapter = builder.build();
ChansonApi api = restAdapter.create(ChansonApi.class);
api.getStreamProgram(new Callback<StreamProgram>() {
@Override
public void success(StreamProgram streamProgram, Response response) {
if(streamProgram!=null){
Log.e("mcheck", "success: "+streamProgram.getProgram());
}
}
@Override
public void failure(RetrofitError error) {
Log.e("mcheck", "failure: ",error);
}
});
问题出在 proguard 设置中。我必须将 TimeConverter 添加到 -keep class list
我想解析时间来自
<monday>
<item>
<time>00:00:00</time>
</item>
...
</monday>
只要 我将物品定义为
@Root(strict = false)
private static class Item {
@Element(name = "time")
@Convert(TimeConverter.class)
private Long time;
}
我的转换器
public class TimeConverter implements org.simpleframework.xml.convert.Converter<Long> {
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
@Override
public Long read(InputNode node) throws Exception {
try {
String value = node.getValue();
return df.parse(value).getTime();
} catch (Exception e) {
Log.e("mcheck", "read: ", e);
return 0L;
}
}
@Override
public void write(OutputNode node, Long value) throws Exception {
try {
String v = df.format(new Date(value));
node.setValue(v);
} catch (Exception e) {
Log.e("mcheck", "write: ", e);
}
}
}
然而,当我解析它时,我收到
retrofit.RetrofitError: java.lang.NumberFormatException: Invalid long: "00:00:00"
由于这个异常没有在转换器中的 try-catch 块中捕获,我假设解析器根本不访问转换器。
我的改装 1.9 调用
OkHttpClient httpClient = new OkHttpClient();
RestAdapter.Builder builder = new RestAdapter.Builder();
builder.setEndpoint(url);
builder.setLogLevel(RestAdapter.LogLevel.FULL);
builder.setConverter(new SimpleXMLConverter());
builder.setClient(new OkClient(httpClient));
RestAdapter restAdapter = builder.build();
ChansonApi api = restAdapter.create(ChansonApi.class);
api.getStreamProgram(new Callback<StreamProgram>() {
@Override
public void success(StreamProgram streamProgram, Response response) {
if(streamProgram!=null){
Log.e("mcheck", "success: "+streamProgram.getProgram());
}
}
@Override
public void failure(RetrofitError error) {
Log.e("mcheck", "failure: ",error);
}
});
问题出在 proguard 设置中。我必须将 TimeConverter 添加到 -keep class list