如何确保UTF8用于共享首选项菜单?我有一个android偏好菜单,允许用户设置他们的名字等等。android,UTF8 - 如何确保UTF8用于共享首选项
我需要知道如何存储在共享偏好将数据转换为UTF-8格式
偏好菜单中的XML使用UTF8编码的文件在res/XML文件所调用的设置布局,看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="@string/name_key"
android:title="@string/hof_name_title"
android:summary="@string/hof_name_summary"
android:defaultValue="Anonymous" />
<CheckBoxPreference
android:key="@string/new_game_preference_key"
android:title="@string/new_game_preference_title"
android:summary="@string/new_game_preference_summary"
android:defaultValue="false" />
</PreferenceScreen>
处理这个是
public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
@Override
protected void onPause() {
super.onPause();
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// Util.log_debug_message("@@@@ Changed prefrence key = " + key);
if (key.equalsIgnoreCase(getString(R.string.name_key))){
update_webserver();
}else if (key.equalsIgnoreCase(getString(R.string.new_game_preference_key))){
update_webserver();
}
}
protected void update_webserver(){
Intent webServerReg = new Intent(this, RegService.class);
webServerReg.putExtra(Config.C2DM_REG_ID_EXTRA, C2DMessaging.getRegistrationId(this));
startService(webServerReg);
}
}
我认为设置<?xml version="1.0" encoding="utf-8"?>
将确保文本将在UTF8编码FO类rmat,但情况并非总是如此。我以各种格式获取价值。一些例子
“\ NFE \ xF1a”, “L \ xFAcia”, “$ '\ xE5 \ XEE'”,
或
“: - $ B- )O: - ): - P = -OB - ): - d:-Q:-X: - : - |!\ N:'(: - * XDo_O:-X:-C:-X:O: - X = -O; - ): - ): - ); - ): - D:OX-(o_O:-V: - @: - V:-X:-Do_O :: -C \ xBF \ xA1 \ xA1 \ xAB \ xBB \ xAE \ xA9^\ xA5 \ xA5?[\ xA2}?\ xA2 \ xA2 \ xA5 \ xA3?$ \ xBF \ xA1 \ xAE \ xA7> < [\ xA3〜?=
\xB0]\xA3?\xA7\xA1\\\xAB\xBB\xAE^``]]||||}{_|
] || \ xB0 \ xB0?“
是的,这是最后一个例子中的一组漏斗。
的问题,我已经开始尝试使用下面的代码
public void update(String regId) throws AuthenticationException {
JSONObject mu_to_send = createJsonToPost(regId, false);
HttpResponse response;
try {
response = HttpRequest.httpPutJSONObject(this,
Config.UPDATE_USER_URL, mu_to_send);
int responseCode = response.getStatusLine().getStatusCode();
String responseJson;
try {
responseJson = EntityUtils.toString(response.getEntity());
String msg = "";
if (responseCode == 201) {
// mobile_user = new
// JSONObject(responseJson).getJSONObject("mobile_user");
// Broadcast.sendBroadcast(this,
// ResponseReceiver.ACTION_RESP,
// Intent.CATEGORY_DEFAULT, Config.C2DM_MESSAGE_EXTRA,
// Config.BROADCAST_WEBSERVER_USER_CREATED);
} else {
msg = "Failed to register with server! HTTP response code = "
+ responseCode;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public JSONObject createJsonToPost(String regId, boolean set_password) {
JSONObject holder = new JSONObject();
JSONObject data = new JSONObject();
try {
data.put("auth", regId);
data.put("name", C2DMessaging.getName(this));
data.put("receive_new_game_notification",
C2DMessaging.getGameStartedNotificationPreference(this));
holder.put("mobile_user", data);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return holder;
}
public static HttpResponse httpPutJSONObject(Context context, String url,
JSONObject data) throws ClientProtocolException, IOException,
AuthenticationException {
DefaultHttpClient httpclient = getHttpClient(context);
HttpPut httpput = new HttpPut(url);
httpput.addHeader("User-Agent", Config.USER_AGENT);
httpput.addHeader("Accept", "application/json");
httpput.addHeader("Content-type", "application/json");
httpput.addHeader(new BasicScheme().authenticate(
getCredentials(context), httpput));
StringEntity se = new StringEntity(data.toString());
httpput.setEntity(se);
return httpclient.execute(httpput);
}
这会导致我的Web服务器上的种种问题,其预计有效发送NMES作为JSON HTTP POST或PUT请求到我的网络服务器时, JSON(即UTF8) 由于JSON应该首先以UTF8编码发送。
所以
1)为什么不<?xml version="1.0" encoding="utf-8"?>
实际设置UTF8编码?什么时候在布局中使用?
2)如何最好地确保我总能得到有效的UTF8格式字符发送到我的Web服务器? 这应该由放置请求还是由保存到共享首选项的代码处理,或由填充json对象的代码或上述 的组合来处理?或者是其他东西?
这是从这个回报率问题Rails 3 - How to handle PG Error incomplete multibyte character
仅供参考 - 我认为设置这样的: “<?XML版本=” 1.0" 编码=“UTF-8 '?>“是指示任何xml解析器,布局文件的内容以UTF-8编码。换句话说,它完全是xml布局文件本身的一个属性,我不相信它在使用该布局时对Android的行为有任何影响... – 2013-01-03 22:20:06