我正在尝试清理将在服务器端发送的应用程序上的一些用户输入。根据服务器设置,在创建或访问文件和文件夹时,可能会使用用户输入的数据。显然,卫生服务也将在服务器上处理,但由于应用程序的性质,我不会和应用程序无法控制。因此,我认为我有责任在我或我的应用程序能够控制的地方拥有端到端的卫生设施。Android - 试图根据偏好消毒用户输入
我试图净化的首选项是一个将传递到服务器的相册名称。
根据Log.d
输出这是正常工作。但是,当我单击首选项字段进行编辑时,它会显示未分类的输入,并将首选项传递给服务器时,它也会显示未分类的输入。
FileUtil.sanitize(stringValue)
是一种简单的方法,用于修剪前导空格和尾随空格,并从字符串中删除../
和..\
,现在是至少。我知道还有其他我需要观看的,只是不是100%他们现在还在。
所以,显而易见的问题是,我错过了什么?在完成这个事件后,是否有事件会覆盖我在这里做出的更改?我感觉有些东西会覆盖我的更改,因为当我在更改后立即获取值时,它会显示正确的消毒数据。
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
} else if (preference.getKey().equals("text_default_album")) {
Log.d(TAG, "default pref: "+preference.getKey()+" value: "+stringValue);
stringValue = FileUtil.sanitize(stringValue);
Log.d(TAG, "default pref: "+preference.getKey()+" value: "+stringValue);
preference.setSummary(stringValue);
SharedPreferences prefs = preference.getSharedPreferences();
String def = prefs.getString("text_default_album", "");
Boolean updated = prefs.edit().putString("text_default_album", stringValue).commit();
String def2 = prefs.getString("text_default_album", "");
Log.d(TAG, "def: "+def);
Log.d(TAG, "def2: "+def2);
Log.d(TAG, "updated: "+updated);
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
在'其他if'分支,请尝试使用'preference.getSharedPreferences()'代替'getSharedPreferences(上下文)'。 –
所以这个改为:'SharedPreferences prefs = preference.getSharedPreferences();'? – Jayrox
我做出了改变,对我所经历的行为没有任何影响,但它确实看起来更好。 – Jayrox