检索阿拉伯文字从EDITTEXT Android中 [英] Retrieving Arabic text from editText in Android
问题描述
我在Android的女巫的应用程序有一个EditText这需要阿拉伯文字。然后在此基础上阿拉伯文字我用PHP进行查询MySQL中。问题是,当我检索来自EDITTEXT MySQL查询阿拉伯文文本返回null,但是当我在程序中(而不是从EDITTEXT retieving吧)手动编写的阿拉伯文文本它的工作原理完美的上帝。这是什么问题任何人能帮助我。 谢谢你。
这是我的PHP code
< PHP
标题(内容类型:text / html的;字符集= UTF-8);
$ CON = mysqli_connect(本地主机,根,,SMD);
//检查连接
如果(mysqli_connect_errno()){
回声无法连接到MySQL:。 mysqli_connect_error();
}
$字= $ _REQUEST ['过滤器'];
// $字=قط;如果我在这里写的阿拉伯语单词手工查询工作正常
mysqli_query($ CON,SET CHARACTER SET UTF8);
$结果= mysqli_query($ CON,SELECT * FROM arwn_synset,arwn_gloss WHERE字='$语,arwn_synset.synset_id_ar = arwn_gloss.synset_id_ar);
而($行= mysqli_fetch_assoc($结果)){
$输出[] = $行;
}
回声json_en code($输出);
?>
这是Android code:
//获取同义词集从用户找
EditText上SynsetEditText =(EditText上)findViewById(R.id.synsetEditText);
。字符串synsetString = SynsetEditText.getText()的toString();
//删除从一开始的空白和结尾的字符串
synsetString = synsetString.trim();
// synsetString =قط;如果我在这里写的阿拉伯语单词手工查询工作正常
myIntent =新的意图(MainActivity.this,AWNActivity.class);
myIntent.putExtra(同义词集合,synsetString);
startActivity(myIntent);
这是在AWNActivity.java:
字符串URL =HTTP://+本地主机+/smd/connectA.php;
JSONasyncTask的AsyncTask =新JSONasyncTask();
asynctask.execute(URL);
私有类JSONasyncTask扩展的AsyncTask<字符串,太虚,JSONArray>
{
@覆盖
保护JSONArray doInBackground(字符串...网址)
{
连接连接=新的连接();
JSONArray jsonArray = connect.ConnectToRESTful(网址[0],synsetString);
返回jsonArray;
}
@覆盖
保护无效onPostExecute(JSONArray JSONArray),其中
{
如果(jsonArray!= NULL)
{
// 做一点事
}
}
}
公共JSONArray ConnectToRESTful(URL字符串,字符串[]单词表)
{
//的同义词集合数据发送
字符串结果=;
ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
的for(int i = 0; I< wordList.length;我++)
nameValuePairs.add(新BasicNameValuePair(过滤器,词表[I]));
InputStream的是= NULL;
// HTTP POST
尝试
{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(URL);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中,UTF-8));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
}
赶上(例外五)
{
Log.e(有一个问题错误的HTTP连接\ñ,e.toString());
}
//转换响应串
尝试
{
的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL)
{
sb.append(行+\ N);
}
is.close();
结果= sb.toString();
}
赶上(例外五)
{
Log.e(有问题的误差将导致\ñ。e.toString());
}
JSONArray jsonArray = NULL;
尝试
{
jsonArray =新JSONArray(结果);
}
赶上(JSONException E)
{
Log.e(有一个问题\ñ,e.toString());
}
返回jsonArray;
}
您应该使用收集 utf8_general_ci
,在数据库表中的字段保存您的阿拉伯语数据
,也可以使用标题(内容类型:text / html的;字符集= UTF-8);
在你的PHP文件的顶部...
I have an application in Android witch has an editText that takes Arabic text. Then based on this Arabic text i make a query in MySQL through php. The problem is that when i retrieve the Arabic text from the editText MySQL query returns null but when i write the Arabic text manually in the program (not retieving it from the editText) it works perfect and god. What is the problem any one can help me. Thanks.
This is my php code
<?php
header("Content-type: text/html; charset=UTF-8");
$con=mysqli_connect("localhost","root","","smd");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$word = $_REQUEST['filter'];
//$word="قط"; if i write here the Arabic word manually the query works fine
mysqli_query($con,"SET CHARACTER SET utf8");
$result = mysqli_query($con,"SELECT * FROM arwn_synset,arwn_gloss WHERE word='$word' AND arwn_synset.synset_id_ar=arwn_gloss.synset_id_ar");
while($row = mysqli_fetch_assoc($result)) {
$output[]=$row;
}
echo json_encode($output);
?>
This is the Android code:
//Getting the synset from the user to look for
EditText SynsetEditText = (EditText) findViewById(R.id.synsetEditText);
String synsetString = SynsetEditText.getText().toString();
//Removing white space from the beginning and end of the string
synsetString=synsetString.trim();
//synsetString="قط"; if i write here the Arabic word manually the query works fine
myIntent = new Intent(MainActivity.this,AWNActivity.class);
myIntent.putExtra("synset", synsetString);
startActivity(myIntent);
This is in AWNActivity.java:
String url="http://"+localHost+"/smd/connectA.php";
JSONasyncTask asynctask=new JSONasyncTask();
asynctask.execute(url);
private class JSONasyncTask extends AsyncTask<String, Void,JSONArray>
{
@Override
protected JSONArray doInBackground(String... urls)
{
Connect connect=new Connect();
JSONArray jsonArray=connect.ConnectToRESTful(urls[0],synsetString);
return jsonArray;
}
@Override
protected void onPostExecute(JSONArray jsonArray)
{
if(jsonArray!=null)
{
// Do something
}
}
}
public JSONArray ConnectToRESTful(String url,String[] wordList)
{
//the synset data to send
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (int i=0;i<wordList.length;i++)
nameValuePairs.add(new BasicNameValuePair("filter",wordList[i]));
InputStream is = null;
//http post
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("There is a problem. Error in http connection\n", e.toString());
}
//convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("There is a problem. Error converting result.\n", e.toString());
}
JSONArray jsonArray = null;
try
{
jsonArray = new JSONArray(result);
}
catch (JSONException e)
{
Log.e("There is a problem.\n", e.toString());
}
return jsonArray;
}
You should use collection utf8_general_ci
" in database table field to save your arabic data
and also use header("Content-type: text/html; charset=UTF-8");
at the top of your php file...
这篇关于检索阿拉伯文字从EDITTEXT Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!