dtpxi88884 2017-04-20 07:27
浏览 52

使用json从android到服务器发送/检索阿拉伯字符(utf 8)

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);
  }
  ?>
  • 写回答

3条回答 默认 最新

  • dsa5233 2017-04-20 10:25
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用