Java 无法识别阿拉伯语字符
我正在使用java和php从android studio中的数据库检索文本。英文文本显示成功,但当我将阿拉伯语添加到数据库时,它在Android中显示为方框。你们能帮我解决这个问题吗 它可以在html浏览器页面中完美地工作。但在安卓系统中,情况并非如此。但是,它的xml文件设置为UTF8。它应该是有效的。有人能找出问题吗 Main.javaJava 无法识别阿拉伯语字符,java,php,android,database,arabic,Java,Php,Android,Database,Arabic,我正在使用java和php从android studio中的数据库检索文本。英文文本显示成功,但当我将阿拉伯语添加到数据库时,它在Android中显示为方框。你们能帮我解决这个问题吗 它可以在html浏览器页面中完美地工作。但在安卓系统中,情况并非如此。但是,它的xml文件设置为UTF8。它应该是有效的。有人能找出问题吗 Main.java public class Main extends AppCompatActivity { //phpconnection declaration Te
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.");
}
}
活动\u 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" />
?>
索引
<?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']);
}
?>
试试这个
它有完整的指南如何在Android中显示阿拉伯语
此外,您还需要在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(“您的阿拉伯文文本));
我的Android Studio是什么意思?你是说IDE预览还是在你的设备上实际运行应用程序?我正在我的设备上运行应用程序。它的操作系统版本是lolipop 5.0。可能是编码错误,你是否尝试从string.xml文件中显示阿拉伯文文本?不!你能指导我如何使用吗在string.xml文件中执行此操作您能试试这个吗,yourTextView.setText(Html.fromHtml(“您的字符”);
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"));