I'm trying to pass parameters to my php script. The data gets passed but always returns "success" as 0. I tested the php by echoing the query after passing values, it works fine and puts all the data. Even var_dump($_POST)
indicates it receives data but it always returns "success" as 0.
Adding parameters:
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
method:
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]
recieved by 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"}
Try this beginning in your php, for my app it did work:
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;
}
(...)
just make sure you are pointing correctly in the httpPost to the php file