我正在尝试将参数传递给我的php脚本。数据传递但总是返回“成功”为0.我通过在传递值后回显查询来测试php,它工作正常并放入所有数据。即使var_dump($_POST)
表示它接收数据,但它总是将“成功”返回为0.
添加参数:
params.add(new BasicNameValuePair("states", states));
params.add(new BasicNameValuePair("min_budget", min_budget));
params.add(new BasicNameValuePair("max_budget", max_budget));
params.add(new BasicNameValuePair("activity", activity));
Log.d("Passing parameters: ", params.toString());
// Check your log cat for JSON response
try {
JSONObject json = jParser.makeHttpRequest(url_all_resorts, "POST", params);
makeHttpRequest
方法:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
Log.d("Passing parameters 2: ", params.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
Log.d("Entity 2: ", httpEntity.toString());
is = httpEntity.getContent();`
logcat的:
Log.d("Passing parameters 2: ", params.toString());
Passing parameters 2:: [states=Himachal Pradesh, min_budget=1,000, max_budget=9,000, activity=Leopard Safari]
收到了android:
Data received:: array(4) {
["states"]=>
string(16) "Himachal Pradesh"
["min_budget"]=>
string(5) "1,000"
["max_budget"]=>
string(5) "9,000"
["activity"]=>
string(14) "Leopard Safari"
}
{"success":0,"message":"No resort found"}
答案 0 :(得分:0)
在你的php中尝试这个开头,对于我的应用程序,它确实有效:
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
$username = "";
$password = "";
//here use your values
if (!empty($request->password)) {
$password = $request->password;
}
if (!empty($request->username)) {
$username = $request->username;
}
(...)
确保你正确地指向httpPost中的php文件