如何確保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