从Android中的editText检索阿拉伯语文本

时间:2014-11-13 13:14:31

标签: php android mysql android-edittext arabic

我在Android中有一个应用程序,它有一个带有阿拉伯语文本的editText。然后基于这个阿拉伯文本我通过PHP在MySQL中进行查询。问题是,当我从editText中检索阿拉伯语文本时,MySQL查询返回null但是当我在程序中手动编写阿拉伯文本时(不是从editText中重新编写它),它完美而且上帝。有什么人可以帮助我的问题是什么。 感谢。

这是我的php代码

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

这是Android代码:

//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); 

这是在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;
}

1 个答案:

答案 0 :(得分:0)

你应该使用集合utf8_general_ci&#34;在数据库表字段中保存您的阿拉伯数据

并在php文件的顶部使用header("Content-type: text/html; charset=UTF-8"); ...