0

I want to send a POST request from android to a PHP application in utf-8 with the below code:

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(Site.SQL_QUERY, "just a test 東京"));

UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params);
urlEncodedFormEntity.setContentEncoding(HTTP.UTF_8);
httppost.setEntity(urlEncodedFormEntity);

HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new inputStreamReader(response.getEntity().getContent(), HTTP.UTF_8));
String result = reader.readLine();

I set encoding to UTF-8, but Japanese characters contained in the string are not displayed normally.

Here is my PHP code(cakePHP). I just dump parameter.

function sqlQuery(){
   var_dump($this->params['form']['sql_query']);exit;
}

It will display something like "just a test □□"

I have also tried with

mb_convert_encoding ($this->params['form']['sql_query'],"UTF-8")

But problem persists...

Someone can help me ? Thank you

2
  • I found the solution ! UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params,HTTP.UTF_8); urlEncodedFormEntity.setContentEncoding(HTTP.UTF_8); httppost.setEntity(urlEncodedFormEntity); Commented Jul 2, 2012 at 9:14
  • what cakephp version are you using? in 2.x its $this->request->data Commented Nov 30, 2012 at 13:42

1 Answer 1

3

This has been the problem Sending UTF-8 data from Android. Your code would work fine except that you will have to encode your String to Base64 . At Server PHP you just decode Base64 String back. It worked for me. I can share if you need the code.

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

1 Comment

I have encoded my parameters in Base64 and now it works ~ thank you

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.