duancan1900 2014-05-02 10:14
浏览 94
已采纳

如何显示阿拉伯语和库尔德语文本而不是问号android

I am developing an app that will display text in three languages. I am getting data from server via PHP scripts. The problem is that arabic and kurdish text shows up as ? marks. I have modified the code to accept utf-8 characters. The data shows up as ? marks even when opened in browser. The collation of the mysql server is utf8. Also I have added kurdish and arabic text statically to my app and it works perfectly. I have also tried utf8_encode() without any results -

public static String executeHttpGet(String url) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = getHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);
//          in = new BufferedReader(new InputStreamReader(response.getEntity()
//                  .getContent()));
            in = new BufferedReader(new InputStreamReader(response.getEntity()
                    .getContent(), "UTF-8"));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");

            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }

            in.close();

            String result = sb.toString();

            return result;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    Log.e("log_tag", "Error converting result " + e.toString());
                    e.printStackTrace();
                }
            }
        }
    }

Is there something wrong in my code? Or do I need to do something else? Thanks in advance.

EDIT: I am using the following php script to encode json data -

$subcat_id = mysql_real_escape_string(intval($_GET["id"]));
$query = mysql_query("select * from subcategory where category_id = $subcat_id")or die(mysql_error());
while($row = mysql_fetch_assoc($query))
$output[]=$row;
print(json_encode($output));

and the collation used on the mysql server is utf8. Thanks again.

  • 写回答

1条回答 默认 最新

  • dongshi2458 2014-05-08 10:00
    关注

    use mysql_query("SET CHARACTER SET 'utf8'", $connect); after making database connection for example -

    $connect = mysql_connect(dbhost, user, password) or 
                             die("MySQL Error: " . mysql_error());
    $db = mysql_select_db(dbname) or die("MySQL Error: " . mysql_error());
    mysql_query("SET CHARACTER SET 'utf8'", $connect)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?