小编shm*_*ula的帖子

在一个字段中反序列化多个类型的JSON

我想反序列化JSON(使用Jackson 1.9.11和RestTemplate 1.0.1),其中一个字段可能具有更多类型含义,例如:

    {"responseId":123,"response":"error"}
Run Code Online (Sandbox Code Playgroud)

要么

    {"responseId":123,"response":{"foo":"bar", ... }}
Run Code Online (Sandbox Code Playgroud)

一个或其他案例可以使用一个特定类型的setter(String od自定义Response类)正常工作,但是当我放入我的实体bean覆盖setter以便能够处理这两种情况时,抛出异常:

Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [xxxx.templates.ExportResponse] and content type [application/json;charset=utf-8]
Run Code Online (Sandbox Code Playgroud)

我正在考虑三个解决方案,但我没有得到任何解决方案:

  • 仅使用String setter并在内部使用ObjectMapper来解组该字符串,如果它不等于"error",但是当JS Array出现时,它不是字符串所以不使用String setter :(.
  • 使用多态类型处理(@JsonTypeInfo注释)与自己的JsonDeserializer扩展 - 我仍然试图理解这一点并实现.
  • 创建HttpMessageConverter列表并放入所有消息转换器,我可以使用.但我认为这一步是不必要的,因为只使用MappingJacksonHttpMessageConverter,我是对的吗?

编辑:现在如何运作

实体bean中的setter:

@JsonDeserialize(using = ResponseDeserializer.class)
public void setResponse(Object responseObject) {
    if(responseObject instanceof Response)
        response = (Response) responseObject;
}
Run Code Online (Sandbox Code Playgroud)

在ResponseDeserializer中反序列化方法:

public Response deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
    Response response = new Response();

    if(JsonToken.START_OBJECT.equals(parser.getCurrentToken())) {
        ObjectMapper mapper = new ObjectMapper(); …
Run Code Online (Sandbox Code Playgroud)

java json jackson resttemplate

9
推荐指数
1
解决办法
6577
查看次数

Android首选项对话框样式

我正在尝试在我的应用程序中联合对话框.所有活动都会延伸AppCompatActivity,因此对话框是重要的,但DialogPreference在我的设置活动中除外.

我正在使用PreferenceActivity,但我也改用它AppCompatActivity,使用PreferenceFragment.它没有任何区别.

所以我尝试为我的SettingsTheme添加自定义样式

<style name="SettingsTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:dialogPreferenceStyle">@style/StyledDialog</item>
</style>

<style name="StyledDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
</style>
Run Code Online (Sandbox Code Playgroud)

但我得到的一切都是这个(我也尝试过alertDialogStyle,alertDialogTheme和dialogTheme),holo对话保持不变

在此输入图像描述

我尝试的另一件事是扩展EditTextPreference和覆盖onPrepareDialogBuilder强制我的主题:

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    super.onPrepareDialogBuilder(builder);

    builder.getContext().setTheme(R.style.StyledDialog);
}
Run Code Online (Sandbox Code Playgroud)

也没有成功.有没有办法在一个应用程序中的对话框是AppCompat联合而不是每个都不同?我认为平台应该解决这个问题,但是,我忘了我在Android上.

编辑:经过一番挖掘,我发现DialogPreference使用android.app.AlertDialog而不是android.support.v7.app.AlertDialog.所以看起来我必须找到一种方法来覆盖Builder DialogPreference.

编辑2:好的,所以关于以前的编辑 - 我一直在使用不支持DialogPreference,所以在添加之后com.android.support:preference-v7:23.1.0我就可以访问另一个类了.但它看起来仍然有些错误和一点点hacky,所以我保留那些旧的对话框并等待支持库更新.我找到的一些有趣的链接是:

android android-appcompat android-theme

6
推荐指数
0
解决办法
1299
查看次数

设置日历实例的时间

我正在尝试将时间设置为Calendar实例,但我遇到了奇怪的行为.我们来看看一些例子:

Calendar c = Calendar.getInstance();
//c.setTime(date);

c.set(date.getYear(), (date.getMonth() - 1), date.getDay());
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Run Code Online (Sandbox Code Playgroud)

当我用setTime()设置时间时,它设置错误的日期.第二个(和已弃用的)集(年,月,日)正常工作.我之前从未见过这个 - 我认为它在两种情况下都使用默认时区,所以它应该是相同的.有人可以向我解释一下吗?

====================编辑:让我们问另一种方式:

Date date = new Date();
Calendar c1 = Calendar.getInstance();
c1.setTime(date);

Calendar c2 = Calendar.getInstance();   
c2.set(date.getYear(), (date.getMonth() - 1), date.getDay());

int day1 = c1.get(Calendar.DAY_OF_WEEK);
int day2 = c2.get(Calendar.DAY_OF_WEEK);

// day1 != day2   ---> I'd like to know - WHY?
Run Code Online (Sandbox Code Playgroud)

所以现在的日期如下:

date: Nov 5, 2013 4:27:02 PM
day1: 3
day2: 1
time: 1383665222638
timezone: Europe/Prague
Run Code Online (Sandbox Code Playgroud)

java

4
推荐指数
1
解决办法
2万
查看次数

onResume在后台或手机睡眠后没有被调用

我有恢复我的应用程序的问题 - 当它被切换到后台因别的东西打开(例如我打电话的意图来打开导航),或者是因为手机睡觉; 返回后(另一个活动关闭或电话被唤醒)我的应用程序仍然在后台,onResume从未被调用(日志中没有错误,没有).为什么会发生这种情况?如何摆脱这种行为,我应该从哪里开始寻找?

public class MainActivity extends ...v4.app.FragmentActivity implements FewMyListeners {
    . . .
    private Locator locator;

@Override
public void onResume() {
    super.onResume();

    try {
        locator = new Locator(this);
    } catch (NoLocationProviderFoundException e) {
        showLocationSettingsDialog();
    }

    refreshLocation();
}

@Override
protected void onPause() {
    if(locator != null) {
        locator.removeUpdates(false);
        locator = null;
    }

    dismissProgressDialog();

    super.onPause();
}
}

public class Locator implements LocationSource, LocationListener { . . . }
Run Code Online (Sandbox Code Playgroud)

android onresume android-fragmentactivity

3
推荐指数
1
解决办法
5927
查看次数