HTTPPost没有发送正确的值

时间:2013-07-04 05:36:35

标签: java php android http-post

这是今天关于Android的第三个问题,这开始变得令人尴尬。我正在尝试向此PHP页面发送HTTP POST请求,我正在尝试从MySQL数据库中检索相册的信息:

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

问题是,我只是将“Access Deniedn”(是的,带有n)作为我的HTTP请求的回复。这是我的android java代码:

以下是为请求设置参数的函数:

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

这是来自JSONParser类的getJSONFromURL函数,它执行实际请求:

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;

    }

我为张贴此事道歉,我是Android的新手,我一直试图绕过它几天。

编辑:经过多次调试后,params确实包含“req”作为名称和“listemedias”相关联。它为什么不起作用?这是编码问题吗?有什么办法可以验证为请求发送的标头吗?

编辑2:经过一些额外的修修补补,我无法弄清楚,我正在扔毛巾。我还能使用其他方法吗?

0 个答案:

没有答案