0

I'm trying to save some text in hindi in my database using a java program, it was a simple text in hindi and it stored in database like this:

 ज़िनà¥à¤¦à¤—ी में अगर कà...

following is my php code

//Normal connection stuff
    ....
    $content_hindi = $_POST['content_hindi'];
    $QueryString = "INSERT INTO contents(content_hindi) VALUES ('$content_hindi')";
    ....
//running query stuff

following is my java code

private void sendData() throws IOException{
    URL url = new URL(Constants.URL_ADDDATA);
    Map<String,Object> params = new LinkedHashMap<>();

    params.put(Constants.COL_HINDI, getString(app.text_hindi));
    StringBuilder postData = new StringBuilder();
    for (Map.Entry<String,Object> param : params.entrySet()) {
        if (postData.length() != 0) postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
    byte[] postDataBytes = postData.toString().getBytes("UTF-8");

    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
    conn.setDoOutput(true);
    conn.getOutputStream().write(postDataBytes);

    Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
    StringBuilder sb = new StringBuilder();
    for (int c; (c = in.read()) >= 0;){
        sb.append((char)c);
    }
    app.log(sb.toString());
}

Is something wrong with my php or java code? I tried to input english string, it works fine.

edit: col is set to utf8_unicode_ci

jpm
  • 1,042
  • 2
  • 12
  • 36
  • Possible duplicate of [How to insert Hindi language in Mysql](http://stackoverflow.com/q/11292898/5447994) and [Storing and displaying unicode string (हिन्दी) using PHP and MySQL](http://stackoverflow.com/a/1198713/5447994) – Thamilhan Dec 15 '16 at 04:53
  • check your database and column's character set first and make sure it is in UTF-8 or higher. – Naga Dec 15 '16 at 04:53
  • @Naga it is utf8_unicode_ci, when I'm directly copying and saving in phpMyadmin console it is working, it is not working when I'm sending data via Java program. – jpm Dec 15 '16 at 04:56
  • In Php, we use `header('Content-Type: text/html; charset=utf-8');` or `` to handle unicodes by the program. – Naga Dec 15 '16 at 05:00
  • @Naga http://pastebin.com/mu3aHjZf – jpm Dec 15 '16 at 05:06

1 Answers1

0

I solved this problem using: mysqli_set_charset

$con=mysqli_connect($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_set_charset($con,"utf8");

$content_hindi = $_POST['content_hindi'];
jpm
  • 1,042
  • 2
  • 12
  • 36