在Android的HttpClient调用时PHP脚本错误 [英] Php script error when called from Android HttpClient
问题描述
我有一个剧本一个问题,不会返回它应该从我的Android应用程序调用时。
I have a problem with a script which doesn't return what it should when called from my Android app.
Android的侧
int pur_user = 3;
URL url = new URL("http://www.example.com/conf/includes/purchase.php");
String result = "";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url.toString());
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
JSONObject jsonObject = new JSONObject();
jsonObject.put("PUR_sku", SKU);
jsonObject.put("PUR_user", pur_user);
pairs.add(new BasicNameValuePair("data", jsonObject.toString()));
httpPost.setEntity(new UrlEncodedFormEntity(pairs,HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
InputStream is = response.getEntity().getContent();
result = convertInputStreamToString(is);
Log.d("SEND", "Response of Send Result : " + result);
我试过3种不同的方式连接到我的Web服务器,而结果总是好的,所以我想这个问题来自于PHP脚本。
I tried 3 different ways to connect to my web server, and the result is always ok, so i guess the issue comes from the PHP script.
PHP端
<?
$auth=0;
include('./connexion.php');
//$data = file_get_contents('php://input');
//$json = json_decode($data,true);
$json = json_decode($this->input->post('data'), true);
if (strip_tags($json['PUR_sku'])=="singleconf" || strip_tags($json['PUR_sku'])=="fiveconf" || strip_tags($json['PUR_sku'])=="tenconf"){
$addcredits = 0;
if (strip_tags($json['PUR_sku'])=="singleconf"){$addcredits = 1;}
if (strip_tags($json['PUR_sku'])=="fiveconf"){$addcredits = 5;}
if (strip_tags($json['PUR_sku'])=="tenconf"){$addcredits = 10;}
if ($addcredits>0){
$S_user = mysqli_query($C,"SELECT USE_id, USE_maxcredits FROM CONF_users WHERE USE_id = '".strip_tags($json['PUR_user'])."' LIMIT 1");
if (mysqli_num_rows($S_user)==1){
$DT_user = mysqli_fetch_assoc($S_user);
$newcredits = $DT_user['USE_maxcredits'] + $addcredits;
$U_user = mysqli_query($C,"UPDATE CONF_users SET USE_maxcredits = '".$newcredits."' WHERE USE_id = '".$DT_user['USE_id']."' LIMIT 1");
$_SESSION['CO_maxcredits'] = $newcredits;
}
}
}
echo "Retour ".print_r($json)." et ".$json['PUR_sku']." et ".$json['PUR_user'];
?>
在logcat中我得到发送结果的回应:与空字符串...
我试着调试,删除所有线路除了最后一个(回声)之一,我得到的logcat正确的反应。
看来问题是本着包括('./ connexion.php'),但我知道该字符串的文件名就可以了,因为我在其他脚本中使用完全相同的之一。
In logcat i get "Response of Send Result : " and empty string... I tried to debug, deleting all lines except the last (echo) one, and i get the right response in logcat. It seems the problem is with line "include('./connexion.php')", but i know the string filename is ok because i use the exact same one in an other script.
我在做什么你知道错了??
Any idea of what i'm doing wrong ??
推荐答案
现在的问题是与你的回声传译.print_r($ JSON)。ET。$ JSON ['PUR_sku'] 等$ json的['PUR_user'];
语句。在这里,的print_r
返回布尔(真)和你的code是指将其转换成字符串。
The problem is with your echo "Retour ".print_r($json)." et ".$json['PUR_sku']." et ".$json['PUR_user'];
statement. Here print_r
returns bool ( true ) and your code means to convert it into string.
下面是解决方案。
$auth=0;
include('./connexion.php');
//$data = file_get_contents('php://input');
//$json = json_decode($data,true);
$json = json_decode($this->input->post('data'));
if (strip_tags($json['PUR_sku'])=="singleconf" || strip_tags($json['PUR_sku'])=="fiveconf" || strip_tags($json['PUR_sku'])=="tenconf"){
$addcredits = 0;
if (strip_tags($json['PUR_sku'])=="singleconf"){$addcredits = 1;}
if (strip_tags($json['PUR_sku'])=="fiveconf"){$addcredits = 5;}
if (strip_tags($json['PUR_sku'])=="tenconf"){$addcredits = 10;}
if ($addcredits>0){
$S_user = mysqli_query($C,"SELECT USE_id, USE_maxcredits FROM CONF_users WHERE USE_id = '".strip_tags($json['PUR_user'])."' LIMIT 1");
if (mysqli_num_rows($S_user)==1){
$DT_user = mysqli_fetch_assoc($S_user);
$newcredits = $DT_user['USE_maxcredits'] + $addcredits;
$U_user = mysqli_query($C,"UPDATE CONF_users SET USE_maxcredits = '".$newcredits."' WHERE USE_id = '".$DT_user['USE_id']."' LIMIT 1");
$_SESSION['CO_maxcredits'] = $newcredits;
}
}
}
echo "Retour ".json_encode($json)." et ".$json['PUR_sku']." et ".$json['PUR_user'];
这篇关于在Android的HttpClient调用时PHP脚本错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!