Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:58:12+00:00 2026-06-16T16:58:12+00:00

How do I ensure UTF8 is used for a shared preference menu? I have

  • 0

How do I ensure UTF8 is used for a shared preference menu? I have an android preferences menu that allows a user to set their name, amongst other things.

I need to know how to convert the data stored in shared preferences into UTF8 format

The preference menu is laid out in xml using utf8 encoding in a file called settings in the res/xml folder and looks like this:

<?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>

The class that handles this is

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);     
    }
}

I assumed that setting <?xml version="1.0" encoding="utf-8"?> would ensure that text would be encoded in UTF8 format but this is not always the case. I am getting values in all sorts of formats. Some examples

“\nFe\xF1a”, “L\xFAcia”, “$’\xE5 \xEE'”,

or

“:-$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?”

Yes that is one loooong set of rubish in the last example.

The problems I have start when trying to send the nmes as a json HTTP POST or PUT request to my webserver using the following code

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);
}

This causes all sorts of issues on my web server which expects valid JSON (i.e. UTF8)
as JSON should be sent encoded in UTF8 in the first place.

So

1) Why doesn’t <?xml version="1.0" encoding="utf-8"?> actually set UTF8 encoding? when used in the layout?

2) How best to ensure that I always get valid UTF8 format characters sent to my web server?
Should this be handled by the put request or by the code that saves to the shared preference or or by the code that populates the json object or perhaps a combination of the above
? or something else?

This is a follow on from this RoR question Rails 3 – How to handle PG Error incomplete multibyte character

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:58:13+00:00Added an answer on June 16, 2026 at 4:58 pm

    The key is to understand the difference between UTF-8 and Unicode.

    • Java processes characters and strings in memory using Unicode. Each character is stored in two bytes.
    • When text is transmitted between processes (eg to a web server) or it is written to/read from disk, the internal representation is converted into an over-the-wire format. This is the encoding or decoding. UTF-8 is the most popular, but other formats include:
      • UTF-16
      • ISO 8859-1

    In your question, you mention that the XML files are encoded in utf-8: That is good, and you will be able to put foreign characters in the files, but that specifies the encoding only for that specific XML file.

    These XML files will be compiled into Android resources and will contain the correct values (you can check it if you like in the debugger, or by preserving the intermediate Java resource files from the build chain).

    The problem is almost certainly where you send data to and receive data from the HTTP server, specifically where that data is converted between the bytes on the network and a Java String. Currently you are not setting it in the request – this can be done as described in the documentation for Apache HTTPClient.

    Although the server might already require/assume this, it’s certainly a good thing to state clearly in the request.

    You also need to ensure that the server (the one in Rails 3 – How to handle PG Error incomplete multibyte character):

    • Is expecting UTF-8
    • Decodes the request using a UTF-8 decoder
    • Encodes the response using UTF-8 encoding

    (Sorry, but I don’t know Ruby on Rails so I don’t know how to specifically help there).

    Back in the Android end, you also need to ensure that your HTTP library is decoding the response with the UTF-8 decoder. If you handle this yourself, ensure that the String constructor you use is this one, and the argument is “utf-8”:

    • public String (byte[] data, String charsetName)

    Once BOTH the client and the server are using UTF-8, your problems will be resolved.

    To help debugging here, I suggest:

    1. A number of logging statements on server and client that print the relevant strings as close as possible to the HTTP code
    2. Running with the client configured to talk through a debugging proxy. Examine the request and response and check that they are indeed UTF-8. Proxies include:

      • Charles
      • WebScarab
      • Fiddler
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to ensure that the user login form for Drupal can not be
I have a feature that uploads a user submitted CSV file into my database
I have an issue with Spark Lists whereby I am trying to ensure that
How can I ensure that information isn't returned from a webmethod or mvc action
How can I ensure that all data that I've erase from the db tables,
I want to ensure that a division of integers is always rounded up if
How does C++ ensure that destructors are called for stack assigned objects? What happens
I want to ensure that a WCF-ServiceClient State will be closed after using the
What can I do to ensure that replication will use latin1 instead of utf-8?
Is there any function that I can use to parse any string to ensure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.