Retrieving Arabic text from editText in Android

2019-09-16 22:14发布

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

1条回答
叼着烟拽天下
2楼-- · 2019-09-16 22:48

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...

查看更多
登录 后发表回答