0

I want to add search function in my android app, but the problem is when i add arabic parameter to url e.g("example.com?q=بلد") it gives null result. although if i add text manually in browser to url it works fine.

        InputStream inputStream = null;
        String contentAsString = null;
        try {
            URL url = new URL("example.com?q=بلد");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();
            inputStream = conn.getInputStream();
            contentAsString = readIt(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

PHP code

    header('Content-Type: text/html; charset=utf-8');

    mysql_select_db($this->get_databaseName(), $this->con);
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8');

    $query="Select * from tbl_posts where post_title=".$_GET['q']";

Data base table field encoding method is utf8_general_ci

In simple words i want to add some arabic text as parameter in url, through which i can query database using php web service.

Shaukat Ali
  • 13
  • 3
  • 9

2 Answers2

0

I think u have to url encode your query before you send it. like this:

String query = URLEncoder.encode("بلد", "utf-8");

URL url = new URL("example.com?q=" + query);

miago
  • 119
  • 1
  • 1
  • 7
0

I have solved the problem with the help of Zesshan khan answer

Final code is

Adnroid

   String str=بلد;
   str=URLEncoder.encode( str, "utf-8")  
   URL url = new URL("example.com?q= "+str);

PHP

  header('Content-Type: text/html; charset=utf-8');

..

 mysql_select_db($this->get_databaseName(), $this->con);

 mysql_query("SET NAMES 'utf8'");

. .

$result=uldecoder($_GET["data"]);

. .

$query="Select * from tbl_posts where post_title=".$result.";                      
Shaukat Ali
  • 13
  • 3
  • 9