This is 3rd question today pertaining to Android, this is starting to get embarrassing. I'm trying to send a HTTP POST request to this PHP page, I'm trying to retrieve information for albums from my MySQL database:
<?php
if (isset($_POST['req']) && $_POST['req'] != '') {
$request = $_POST['req'];
// Including the handler for the database
require_once 'include/db_functions.php';
$db = new db_functions();
// Array for response
$response = array("req" => $request, "success" => 0, "error" => 0);
// Check the request type
// Obtains a list of medias for the listview.
if ($request == 'listemedias'){
$mediaList = $db->getMediasList();
// Parse the media list
for($i = 0; $i < count($mediaList); $i++){
$response["mediaList"][$i]["id"] = $mediaList[$i]['idmedia'];
$response["mediaList"][$i]["titre"] = $mediaList[$i]['titre'];
$response["mediaList"][$i]["annee"] = $mediaList[$i]['annee'];
$response["mediaList"][$i]["duree"] = $mediaList[$i]['duree'];
$response["mediaList"][$i]["jaquette"] = $mediaList[$i]['jaquette'];
}
$response["success"] = 1;
echo json_encode($response);
exit;
}
// Obtains specific informations for a specific media.
if ($request == 'media') {
if (isset($_POST['no']) && $_POST['no'] != '') {
$no = $_POST['no'];
$media = $db->getMedia($no);
if ($media != false){
$response["success"] = 1;
$response["media"]["id"] = $media["idmedia"];
$response["media"]["titre"] = $media["titre"];
$response["media"]["annee"] = $media["annee"];
$response["media"]["duree"] = $media["duree"];
$response["media"]["jaquette"] = $media["jaquette"];
$response["media"]["genre"] = $media["nomgenre"];
$response["media"]["editeur"] = $media["nomediteur"];
$response["media"]["collection"] = $media["nomcollection"];
} else {
echo 'Media does not exist';
exit;
}
echo json_encode($response);
exit;
} else {
echo 'Access Denied';
exit;
}
}
} else {
echo 'Access Denied';
exit;
}
?>
The issue is, I'm only getting "Access Deniedn" (Yes, with the n) as a reply from my HTTP requests. Here is my android java code:
Here is the function that sets the parameters for the request:
public JSONObject getMediaList(){
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
// We add the request name to get all medias from the API service
params.add(new BasicNameValuePair("req","listemedias"));
// We get the JSON Object from the API service
JSONObject json = jsonParser.getJSONFromUrl(url, params);
return json;
}
And here is the getJSONFromURL function from the JSONParser class which does the actual request:
public JSONObject getJSONFromUrl (String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
I apologize for posting about this, I'm new to Android, and I've been trying to wrap my head around it for a couple of days.
EDIT: After debugging a bunch of times, params do contain "req" as a name and "listemedias" associated to it. Why isn't it working at all? Is it an encoding problem? Is there any way I can verify the header sent for the request?
EDIT2: After some extra tinkering, I just can't figure it out, I'm throwing the towel. Is there any other method I can use that actually works?