【发布时间】:2015-07-08 15:50:01
【问题描述】:
我遇到了一个问题,该脚本在从我的 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 中,我得到“发送结果的响应:”和空字符串... 我尝试调试,删除除最后一个(回显)之外的所有行,我在 logcat 中得到了正确的响应。 似乎问题出在“include('./connexion.php')”行,但我知道字符串文件名没问题,因为我在另一个脚本中使用了完全相同的文件名。
知道我做错了什么吗??
【问题讨论】:
-
你有 3 种方式连接服务器是什么意思?
-
一个带有 HttpUrlConnection,另一个带有 HttpClient 以及发送 JsonObject 的不同方式,而这个
-
使用 HttpUrlConnection 因为 HttpClient 已被弃用
-
@Bub 我也试过 HttpUrlConnection,但问题不在这里
标签: php android httpclient httpresponse jsonobject