2

This is a follow up question to Android with php: Saving utf-8 string to MySQL

I am using the Gson library in Android to convert a class instance to a JSON string. This JSON string is then uploaded with HttpPost to a PHP script to save it to my MySQL DB.

Initialy I thought the problem was with php or MySQL, but it now appears to be my HttpPost code sequence not properly encoding the JSON to UTF-8. If I debug my code and do a POST from within Chrome with JSON string, it works perfectly.

My MySQL DB is now formatted as UTF-8 (utf8_general_ci).

Here is my HttpPost code:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

ArrayList<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("questionnaire", json));

httppost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

The problem might be in httppost.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8")); , but I am not sure how else to inform my PHP script that I am sending JSON in UTF-8 format.

As an example "can't get there" is correctly encoded by Android as "can/u0027t get there" when the JSON strig is created by Gson. After uploading the JSON to PHP it is saved as "canu0027t get there" without the "\" in the MySQL DB.

So how can I properly label/mark/identify this JSON string as UTF-8 format in Android so that it is saved correctly?

1
  • What must be the request and what you are sending. Please post it here otherwise, your question is vague. Commented Oct 9, 2012 at 12:31

2 Answers 2

4

You have to escape (e.g. mysql_real_escape_string()) the json string in your php code before saving it into the database. Your Android code is just correct.

Sign up to request clarification or add additional context in comments.

Comments

-1

This is the solution:

StringEntity se = new StringEntity(jObjEnvio.toString(), HTTP.UTF_8);

If this dont work do clean and fix the project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.