0

这是今天关于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;
}
?>

问题是,我只收到“拒绝访问”(是的,带有 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 新手,几天来我一直在努力解决这个问题。

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

EDIT2:经过一些额外的修补,我就是想不通,我要扔毛巾了。有没有其他我可以使用的真正有效的方法?

4

0 回答 0