您现在的位置是:首页 > 电脑 > 

eclipse android使用HTTP将java值传递给php(eclipse android pass a java value to php by using HTTP)

2025-07-19 03:52:59
eclipse android使用HTTP将java值传递给php(eclipse android pass a java value to php by using HTTP) 我想将变量q从java传递给php。 String q = "select author from books where 1"; try{ Htt
eclipse android使用HTTP将java值传递给php(eclipse android pass a java value to php by using HTTP)

我想将变量q从java传递给php。

String q = "select author from books where 1"; try{ HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://www2.XXXX./XXXX/X.php?qy="+q); //"http://10.0.2.2/tut.php", http://www.XXXX/XXXX/tut.php HttpRespe respe = (httpPost); HttpEntity entity = respe.getEntity(); is = entity.getContent(); }catch(Exception e){ e.printStackTrace(); println("Exception 1 caught"); }

但是,php文件无法从java(php正确连接到mysql)获取值。

php编码:

<?php $con=mysql_connect("XXX.XXX","XX","XXX"); mysql_select_db("XX",$con); $st = $_GET['qy']; $r = mysql_query("$st"); while($row=mysql_fetch_array($r)) { $out[]=$row; } print(json_encode($out)); mysql_close($con); ?>

我发现如果我只是将表名传递给php,它就可以了。 但是如果传递的变量变得更长,它就会陷入困境。 我怎样才能解决这个问题? 如何将多个变量传递给php(即mysql_query("select $_GET['col'] from $_GET['table'] where $_GET['condition']"); )?

I want to pass the variable q from java to php.

String q = "select author from books where 1"; try{ HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://www2.XXXX./XXXX/X.php?qy="+q); //"http://10.0.2.2/tut.php", http://www.XXXX/XXXX/tut.php HttpRespe respe = (httpPost); HttpEntity entity = respe.getEntity(); is = entity.getContent(); }catch(Exception e){ e.printStackTrace(); println("Exception 1 caught"); }

However, the php file cannot get the value from java(php connected to mysql correctly).

php coding:

<?php $con=mysql_connect("XXX.XXX","XX","XXX"); mysql_select_db("XX",$con); $st = $_GET['qy']; $r = mysql_query("$st"); while($row=mysql_fetch_array($r)) { $out[]=$row; } print(json_encode($out)); mysql_close($con); ?>

I found that if I just pass the table name to php, it works. But if the passing variable become longer, it went to caught one. How can I fix this? How about passing more than one variable to php ( mysql_query("select $_GET['col'] from $_GET['table'] where $_GET['condition']");)?

最满意答案

使用Post而不是Get,它更安全。

像这样的东西:

String col = "author"; String table = "books"; String condition = "1"; try{ List<ameValuePair> params = new ArrayList<ameValuePair>(); params.add(new BasicameValuePair("col", col)); params.add(new BasicameValuePair("table", table)); params.add(new BasicameValuePair("condition", condition)); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://www2.XXXX./XXXX/X.php"); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpRespe respe = (httpPost); HttpEntity entity = respe.getEntity(); is = entity.getContent(); }catch(Exception e){ e.printStackTrace(); println("Exception 1 caught"); }

PHP:

<?php if (isset($_POST['col']) && isset($_POST['table']) && isset($_POST['condition'])){ $columname= $_POST['col']; $tableame = $_POST['table']; $condition = $_POST['condition']; $dbh=mysql_connect ("localhost", "username", "password") or die('Cannot connect to the database because: ' . mysql_error()); mysql_select_db ("database_name"); $sql=mysql_query("select '$columname' from '$tableame' where '$condition'"); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print(json_encode($output)); mysql_close(); } ?>

它工作(由mandi yeung)

Use Post instead of Get, it's more secure.

Something like this:

String col = "author"; String table = "books"; String condition = "1"; try{ List<ameValuePair> params = new ArrayList<ameValuePair>(); params.add(new BasicameValuePair("col", col)); params.add(new BasicameValuePair("table", table)); params.add(new BasicameValuePair("condition", condition)); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://www2.XXXX./XXXX/X.php"); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpRespe respe = (httpPost); HttpEntity entity = respe.getEntity(); is = entity.getContent(); }catch(Exception e){ e.printStackTrace(); println("Exception 1 caught"); }

PHP:

<?php if (isset($_POST['col']) && isset($_POST['table']) && isset($_POST['condition'])){ $columname= $_POST['col']; $tableame = $_POST['table']; $condition = $_POST['condition']; $dbh=mysql_connect ("localhost", "username", "password") or die('Cannot connect to the database because: ' . mysql_error()); mysql_select_db ("database_name"); $sql=mysql_query("select '$columname' from '$tableame' where '$condition'"); while($row=mysql_fetch_assoc($sql)) $output[]=$row; print(json_encode($output)); mysql_close(); } ?>

it works(by mandi yeung)

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/654072.html

相关标签:无
上传时间: 2023-07-28 12:52:06
留言与评论(共有 20 条评论)
本站网友 椰林树影
4分钟前 发表
//www2.XXXX./XXXX/X.php"); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpRespe respe = (httpPost); HttpEntity entity = respe.getEntity(); is = entity.getContent(); }catch(Exception e){ e.printStackTrace(); println("Exception 1 caught"); } PHP: <?php if (isset($_POST['col']) && isset($_POST['table']) && isset($_POST['condition'])){ $columname= $_POST['col']; $tableame = $_POST['table']; $condition = $_POST['condition']; $dbh=mysql_connect ("localhost"
本站网友 石决明的功效
22分钟前 发表
it works. But if the passing variable become longer
本站网友 子时是几点到几点
6分钟前 发表
it went to caught one. How can I fix this? How about passing more than one variable to php ( mysql_query("select $_GET['col'] from $_GET['table'] where $_GET['condition']");)? 最满意答案 使用Post而不是Get
本站网友 家装市场分析
7分钟前 发表
it's more secure. Something like this
本站网友 lingo下载
6分钟前 发表
"XX"
本站网友 理财新趋势
8分钟前 发表
col)); params.add(new BasicameValuePair("table"
本站网友 汇编编译器
4分钟前 发表
它就可以了
本站网友 普通
21分钟前 发表
它更安全
本站网友 保德新闻
25分钟前 发表
$con); $st = $_GET['qy']; $r = mysql_query("$st"); while($row=mysql_fetch_array($r)) { $out[]=$row; } print(json_encode($out)); mysql_close($con); ?> I found that if I just pass the table name to php
本站网友 项目路演
20分钟前 发表
"password") or die('Cannot connect to the database because
本站网友 彼得潘与辛德瑞拉
14分钟前 发表
//www.XXXX/XXXX/tut.php HttpRespe respe = (httpPost); HttpEntity entity = respe.getEntity(); is = entity.getContent(); }catch(Exception e){ e.printStackTrace(); println("Exception 1 caught"); } However
本站网友 五花肉价格
26分钟前 发表
//www2.XXXX./XXXX/X.php?qy="+q); //"http
本站网友 戊二醛
28分钟前 发表
http
本站网友 女人外皮
2分钟前 发表
//www2.XXXX./XXXX/X.php"); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpRespe respe = (httpPost); HttpEntity entity = respe.getEntity(); is = entity.getContent(); }catch(Exception e){ e.printStackTrace(); println("Exception 1 caught"); } PHP
本站网友 北京丰台总部基地
15分钟前 发表
<?php if (isset($_POST['col']) && isset($_POST['table']) && isset($_POST['condition'])){ $columname= $_POST['col']; $tableame = $_POST['table']; $condition = $_POST['condition']; $dbh=mysql_connect ("localhost"
本站网友 产妇吃什么好
24分钟前 发表
the php file cannot get the value from java(php connected to mysql correctly). php coding
本站网友 兰蔻金纯卓颜系列
15分钟前 发表
http
本站网友 万科拿地
16分钟前 发表
"XXX"); mysql_select_db("XX"
本站网友 祁连山招标网
13分钟前 发表
php编码: <?php $con=mysql_connect("XXX.XXX"