簡體   English   中英

Android HttpPost完全不發送POST值

[英]Android HttpPost does not send POST values at all

我正在嘗試對以下PHP代碼執行Http請求:

<?php

var_dump($_POST);

?>

這是我發送請求的Android Java代碼:

        String stringURL = "http://*I EDITED OUT MY IP*/mediatheque_api";

        List<NameValuePair> postVars = new ArrayList<NameValuePair>(2);

        postVars.add(new BasicNameValuePair("req","listemedias"));
        postVars.add(new BasicNameValuePair("no","12345"));

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(stringURL);
            httpPost.setEntity(new UrlEncodedFormEntity(postVars));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            if (httpEntity != null){
                System.out.println("Not Empty");
                String responseBody = EntityUtils.toString(httpEntity);
                System.out.println(responseBody);
            } else {
                System.out.println("Empty");
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

這是LogCat中輸出的內容:

07-04 17:13:15.274: I/System.out(20547): Not Empty
07-04 17:13:15.274: I/System.out(20547): array(0) {
07-04 17:13:15.274: I/System.out(20547): }

如注釋中所建議,我回顯了$ _SERVER ['REQUEST_METHOD']的值,它表示我正在請求GET而不是POST。 問題是,我的Apache服務器的配置沒有被觸及,我顯然正在執行POST請求。

似乎我的HttpPost沒有發送任何POST變量,即使我想通過URL對其進行編碼。 如果僅使用瀏覽器轉到該頁面,我也會得到array(0){},因為請求中沒有任何POST變量。 從昨天開始,我一直在嘗試修改它,但是我只是弄不清楚我在做什么錯。 有什么問題

我發現什么不適用於我的代碼。 此行特別是:

String stringURL = "http://*I EDITED OUT MY IP*/mediatheque_api";

我從未正確地指向包含index.php文件的目錄,因此導致HTTP / 1.1 301永久移動響應,該響應會將我重定向到http:// 我已編輯IP / mediatheque_api /。 我忘了加上最后的斜線。 現在可以正常工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM