0

I'm trying to store Arabic characters in a database on server. But when I add something it will be displayed as ???????????? in the database! I tried too add from my php to check if the problem from the php file, but it added the text correctly in database. So the error from android and json, how can I make the encoding of json to utf8 in android?

Here's my code:

    class AddStories extends AsyncTask<String, String, String> {


    protected String doInBackground(String... args) {


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Caregiver_ID", ID));
        params.add(new BasicNameValuePair("Title", "قصة"));
        params.add(new BasicNameValuePair("Story", "قصتي بدأت عندما"));

        JSONObject json = jsonParser.makeHttpRequest(url_add_story,"POST", params);

        Log.d("Create Response", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                suc=1;

                sucess();


            }
            else {
                suc=0;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
}

PHP for adding:

  <?php
  header("Content-Type: text/html; charset=utf-8");


 // array for JSON response
 $response = array();

 // check for required fields
 if (isset($_POST['Caregiver_ID'])  && isset($_POST['Title'])&& isset($_POST['Story'])) {

$Caregiver_ID = $_POST['Caregiver_ID'];
$Title = $_POST['Title'];
$Story = $_POST['Story'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();
mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');

// mysql inserting a new row
$result = mysql_query("INSERT INTO Stories(Caregiver_ID, Title, Story) VALUES('$Caregiver_ID', '$Title','$Story')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "glossary successfully created.";

    // echoing JSON response
    echo json_encode($response,JSON_UNESCAPED_UNICODE );
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response );
}
?>

Also when I tried to retrieve from android I got this for the text:

  &#1606;&#1577;&#1610;&#1609;&#1572;&#1578;&#1610;&#1604;&#1575;&#1604;&#1575;&#1572;&#1578;&#1610;&#1604;&#1575;&#1575;&#1585;&#1604;&#1575;&#1576;&#1575;&#1585;&#1575;&#1576;&#1610;-&#1575;&#1604;&#1575;&#1604;&#1575;&#1604;&#1578;&#1593;&#1575;

while its retrieved correctly in php.

PHP code

  <?php
  header("Content-Type: text/html; charset=utf-8");

  $response = array();
  require_once __DIR__ . '/db_connect.php';
  $db = new DB_CONNECT();
  $result = mysql_query("SELECT *FROM Stories ") or die(mysql_error());
  mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');

  if (mysql_num_rows($result) > 0) {
  $response["story"] = array();

while ($row = mysql_fetch_array($result)) {
    $story = array();
    $story ["Caregiver_ID"] = $row["Caregiver_ID"];
    $story ["Title"] = mb_convert_encoding($row["Title"],'HTML-ENTITIES','utf-8');
    $story ["Story"] = mb_convert_encoding($row["Story"],'HTML-ENTITIES','utf-8');

    array_push($response["story"], $story);
}

$response["success"] = 1;
echo json_encode($response);
  } else {
// no products found
$response["success"] = 0;
$response["message"] = "No locations found";

// echo no users JSON
echo json_encode($response);
  }
  ?>
Lama Tatwany
  • 95
  • 5
  • 16
  • [Refer this](http://stackoverflow.com/questions/7962704/how-to-support-arabic-text-in-android) – Nilay Vishwakarma Apr 20 '17 at 07:32
  • This worked for me. Have you added these lines `set character_set_server='utf8'` and `set names 'utf8'` before your sql insert query in your PHP side – Naz141 Apr 20 '17 at 07:56
  • Think that android uses alredy utf-8. Please do a test where you just let php echo the json received. Then compare in android. – greenapps Apr 20 '17 at 07:59
  • @Naz141 yes I have added them and that works fine if I added data from PHP, but when I add from android it became ????... – Lama Tatwany Apr 21 '17 at 10:21
  • @greenapps I think the problem is in android, because when I retrieve the text, also its decoded. if you please can check my post I updated it. – Lama Tatwany Apr 21 '17 at 11:22
  • `I'm trying to store Arabic characters` I do not see Arabic characters in your post. Dont you think you should have them in your code? – greenapps Apr 21 '17 at 15:42
  • @greenapps the Arabic text is taken from an edittext, the user enters it from the application. – Lama Tatwany Apr 21 '17 at 15:45
  • Put it in your code. Do you think we all can enter Arabic text? Post reproducable code here. – greenapps Apr 21 '17 at 15:48
  • Ok. But where are the test results? I asked for it:`Please do a test where you just let php echo the json received. `. Show the php script you used. – greenapps Apr 22 '17 at 11:14
  • @greenapps updated it – Lama Tatwany Apr 22 '17 at 11:21
  • ??? Now you have two php scripts with database code. And no one is echoing the input variables in the response array. Please make a little script that just does that. – greenapps Apr 22 '17 at 11:24

3 Answers3

0

Try below code in android

ByteArrayOutputStream baos = new ByteArrayOutputStream();

try {
    response = client.execute(httpGet);
    HttpEntity entity = response.getEntity();
    InputStream stream = entity.getContent();
    byte[] buffer = new byte[1024];
    int nread;
    while ((nread = stream.read(buffer)) > 0) {
        baos.write(buffer, 0, nread);             
    }
} catch (ClientProtocolException | IOException e) {
}
//Create a JSON from the String that was return.
JSONObject jsonObject = new JSONObject();
try {
    String jsonText = new String(baos.toByteArray(), StandardCharsets.UTF_8);
    jsonObject = new JSONObject(jsonText);
Arsalan Khan
  • 403
  • 3
  • 11
0

You have to put N before the columns which you need to add the Arabic text. For example N'مختار'.

0

According to your code,you should change this line $result = mysql_query("INSERT INTO Stories(Caregiver_ID, Title, Story) VALUES('$Caregiver_ID', '$Title','$Story')"); ........ ..... ... to be $result = mysql_query("INSERT INTO Stories(Caregiver_ID, Title, Story) VALUES('$Caregiver_ID', N'$Title',N'$Story')");