java - 无法识别阿拉伯字符

标签 java php android database arabic

我正在使用 java 和 php 从 android studio 的数据库中检索文本。英文文本成功显示,但是当我将阿拉伯语添加到数据库时,它在 Android 中显示为框。你们能帮我解决这个问题吗?

它在 html 浏览器页面中完美运行。但在android中它没有。但是它的 xml 文件设置为 UTF8。它应该工作。谁能找出问题所在?

主.java

  public class Main extends AppCompatActivity {
//phpconnection declaration
TextView resultView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //phpconnection starts from here

    StrictMode.enableDefaults();

    resultView = (TextView) findViewById(R.id.textView);

    getData();
//button

    Button insert=(Button) findViewById(R.id.button);

    insert.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub

            getData();
        }
    });
    //phpconnection ends here
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



//phpconnection again starts
public void getData() {
    String result = "ERROR!\n Please turn on mobile data or Wi-Fi in settings";
    InputStream isr = null;

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost ("http://balochplay.zz.mu/test/index.php");
        HttpResponse response = httpclient.execute (httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
    }
    catch(Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
        resultView.setText("Couldn't connect to database");
    }

    //convert response to string

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        isr.close();

        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
    }

    //parse json data
    try {
        resultView.setText(result);
    }
    catch(Exception e) {
        Log.e("log_tag", "Couldn't set text.");
    }

}

activity_main.xml

   <TextView
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:singleLine="false"
    android:id="@+id/textView"
    android:text="Error! \nPlease Connect to Internet"
    android:gravity="center"
    android:textAlignment="center"
    android:layout_below="@+id/imageView3"
    android:textColor="#ffffff"
    android:textSize="25sp"
    android:autoText="false"
    android:typeface="monospace"
    android:layout_above="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:nestedScrollingEnabled="false"
    android:focusable="true"
    android:layout_marginTop="20dp" />

请指导我如何修复它。

我在论坛上看到过其他类似的问题,但我试过了,但对我没有用。


我的数据库设置是:

表排序规则为utf8_general_ci,类型为MyISAM,其中列(id, name):类型为int 用于 idmediumtext 用于 Name 并且排序规则是 utf8_general_ci

例如。输出应该是 السلام عليكم 但它是 لببي

php代码

dp

<?php
 //create connection
$con=mysqli_connect    ("mysql.hostinger.ph","u242667358_abc","123456","u242667358_test");


//check connection
if (mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

?>

索引

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

require_once('db.php');

$query = "SELECT * FROM BalochiNames ORDER BY RAND() LIMIT 1";   

$result = mysqli_query($con, $query);

while ($row = mysqli_fetch_array($result)) {

echo stripslashes($row['BalochiName']);

}

?>

最佳答案

试试这个 Arabic Language in Android

它有完整的指南如何在 Android 中显示阿拉伯语。

您还需要在给定 here 的 Android 项目中包含一些类. 遵循指南,您就会知道自己必须做什么。

一个简单的例子

AssetManager manager=this.getAssets();
manager.open("tahoma.ttf");
TextView tv=(TextView)this.findViewById(R.id.textView);
tv.setTypeface(Typeface.createFromAsset(manager, "tahoma.ttf"));
tv.setTextSize(50f);
tv.setText(ArabicUtilities.reshape(" الحمد لله hello"));

关于java - 无法识别阿拉伯字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32146752/

相关文章:

java - Maven从不同存储库的一个插件下载文件

java - 显示我的 GET 端点的特定 JSON 响应

php - 在 mySql 中连接多个表

javascript - parse_str 函数不适用于查询字符串

javascript - 从 API 获取数据时如何存储数据

android - 可以在android中的edittext旁边添加语音操作按钮吗

java - 无法在 servlet init 方法中构建 session 工厂

java - 无法解析 Android list 中 Google Play 服务的符号

php - 适用于本地 (MAMP) 和实时服务器的绝对 URL

android - 使用 onTouchEvent 时无法拖动某些区域