从Android HttpClient调用Php脚本错误



我有一个脚本的问题,它不返回它应该从我的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脚本。

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中我得到"发送结果的响应:"和空字符串…我试着调试,删除除最后(echo)一行之外的所有行,我在logcat中得到正确的响应。似乎问题是行"包括('./connection .php')",但我知道字符串文件名是可以的,因为我在另一个脚本中使用完全相同的。

你知道我做错了什么吗?

问题是您的echo "Retour ".print_r($json)." et ".$json['PUR_sku']." et ".$json['PUR_user'];语句。这里print_r返回bool (true),您的代码意味着将其转换为字符串。

这就是解决方案。

$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'];

相关内容

  • 没有找到相关文章

最新更新