如何将url中的阿拉伯语文本作为参数从android app发送到php webservice
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.
我想在我的Android应用程序中添加搜索功能,但问题是当我向网址添加阿拉伯语参数时(例如) “example.com?q=بلد”)它给出了null结果。 虽然如果我在浏览器中手动添加文本到url它工作正常。 p>
InputStream inputStream = null;
String contentAsString = null;
尝试{
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();
}
}
}
code> pre>
PHP代码 p>
\ n
header('Content-Type:text / html; charset = utf-8');
mysql_select_db($ this-> get_databaseName(),$ this-> con); \ n mysql_query(“SET NAMES'utf8'”);
mysql_query('SET CHARACTER SET utf8');
$ query =“select * from tbl_posts where post_title =”。$ _ GET ['q']“;
code> pre>
数据 基表字段编码方法是utf8_general_ci p>
简单来说,我想在url中添加一些阿拉伯语文本作为参数,通过它我可以使用php web服务查询数据库。 p>
DIV>
While sending such kind of special characters to server you need to encode such input with the help of URLEncoder.encode(your_input, "utf-8") and while receiving such kind of data first of all you need to decode it with the help of URLDecoder.decode(your_data, "utf-8")
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);
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.";