emen
emen

Reputation: 170

json utf-8 encoding for android

i have some persian(utf8) words on my database for my android app and i am using json for show database information in android app.
I have following code on Databasemanager.php:

function getMusics()
{
    $connection = mysqli_connect(DatabaseManager::HOST_NAME, DatabaseManager::USER_NAME, DatabaseManager::PASSWORD, DatabaseManager::DATABASE_NAME);
    $sqlQuery = "SELECT * FROM musics where active = 1 order by date desc;";
    $result = $connection->query($sqlQuery);
    $musicsArray = array();
    if ($result->num_rows > 0) {
        for ($i = 0; $i < $result->num_rows; $i++) {
            $musicsArray[$i] = $result->fetch_assoc();
        }
    }
    echo json_encode($musicsArray);
}

and in GetMusic.php for get json information :

<?php
include "DatabaseManager.php";
$databaseManager = new DatabaseManager();
$databaseManager->getMusics();

but i have bellow jsons on output :

{"id":"3","name":"???","artist":"????? ????","like_count":"1","comment_count":"0","dl_link":null,"photo":"http:\/\/192.168.88.6\/musicarea\/photos\/3.jpg","active":"1","date":"2017-08-04 00:00:00"}

how can i solve it ??

on output(i just use persian language at 2 last rows):enter image description here on the app:
enter image description here
on php my admin:
enter image description here

Upvotes: 1

Views: 762

Answers (3)

Droidman
Droidman

Reputation: 11608

You should set the default client charset to UTF-8. Use mysqli::set_charset OR mysqli_set_charset.

See https://www.php.net/manual/en/mysqli.set-charset.php for more info.

Upvotes: 1

Orvenito
Orvenito

Reputation: 437

EDIT

function getMusics()
{
    $connection = mysqli_connect(DatabaseManager::HOST_NAME, DatabaseManager::USER_NAME, DatabaseManager::PASSWORD, DatabaseManager::DATABASE_NAME);
    $sqlQuery = "SELECT * FROM musics where active = 1 order by date desc;";
    $result = $connection->query($sqlQuery);
    $musicsArray = array();
    if ($result->num_rows > 0) {
        for ($i = 0; $i < $result->num_rows; $i++) {
            $musicsArray[$i] = $result->fetch_assoc();
        }
    }
   header("Content-type: application/json; charset=utf-8");
    echo json_encode($musicsArray);
}

Upvotes: 1

Basil Battikhi
Basil Battikhi

Reputation: 2668

You have to add the header that it tells the client the response is encoding with utf-8

header('Content-Type: text/html; charset=utf-8');

Upvotes: 1

Related Questions