无法识别阿拉伯字符

Arabic characters are not recognized

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

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

Main.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" />

请指导我如何修复它。

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


我的数据库设置是:

table 归类为 utf8_general_ci,其类型为 MyISAM,其中的列 (id, name):类型为 int for idmediumtext for 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"));

String appname = String.valueOf(Html.fromHtml("your Text in Arabic));