无法识别阿拉伯字符

时间:2015-08-21 18:07:03

标签: java php android database arabic

我正在使用java和php从android studio中的数据库中检索文本。英语文本显示成功但是当我向数据库添加阿拉伯语时,它在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" />

请指导我如何修复它。

我在论坛上看过其他类似的问题,但是它对我没用。

我的数据库设置为:

表格排名为utf8_general_ci,其类型为MyISAM,其中的列为(id, name):类型为 int for id mediumtext名称,排序规则为utf8_general_ci

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

phpcode

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();
}

&GT;

索引

<?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']);

}

?>

2 个答案:

答案 0 :(得分:1)

试试这个Arabic Language in Android

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

此外,您需要在{$ 3}}的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"));

答案 1 :(得分:0)

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