Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

php - Retrieving Arabic text from editText in Android

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
", 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 + "
");  
        }
        is.close(); 

        result=sb.toString();
    }
    catch(Exception e)
    {
        Log.e("There is a problem. Error converting result.
", e.toString());
    }

    JSONArray jsonArray = null;   
    try
    {
        jsonArray = new JSONArray(result);
    } 
    catch (JSONException e) 
    { 
        Log.e("There is a problem.
", e.toString());
    }
    return jsonArray;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...