Php script error when called from Android HttpClient

时间:2015-07-08 15:50:01

标签: php android httpclient httpresponse jsonobject

I have a problem with a script which doesn't return what it should when called from my Android app.

Android side

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);

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 side

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

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 ??

1 个答案:

答案 0 :(得分:0)

问题在于您的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'];