POST 参数为空,我做错了什么? HttpURLConnection / Android / Java

POST params empty, what am I doing wrong? HttpURLConnection / Android / Java

下面的代码显示了一个方法 downloadUrl(),它接受一个字符串,"myurl," 作为它的参数。我只发送了两个可能的 url,每个方法的行为都不同。

当 myurl = URL1 时,它使用 GET 请求并且一切正常。

然而,当 myurl = URL2 时,它使用 POST 请求,并且 php 页面的响应表明随请求发送的 post 参数为空。你可以看到我设置 POST 参数的行,所以我不明白为什么它不发送任何参数?!

感谢您的帮助! -亚当.

private String downloadUrl(String myurl) throws IOException {

        InputStream is = null;

        String response = "";

        try {
            URL urlObject = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) urlObject.openConnection();

            // find out if there's a way to incorporate these timeouts into the progress bar
            // and what they mean for shitty network situations


            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setDoInput(true);

            // INSERTED QUICK CHECK TO SEE WHICH URL WE ARE LOADING FROM
            // it's important because one is GET, and one is POST


            if (myurl.equals(url2)){

                Log.i(TAG, "dlurl() in async recognizes we are doing pre-call");



                conn.setRequestMethod("POST");
                conn.setDoOutput(true);

                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));

                String postParams = "?phone=" + phone;

                writer.write(postParams);

                Log.i(TAG, "we're adding " + postParams + "to " + urlObject);

                writer.flush();
                writer.close();
                os.close();


            }

            else {

                conn.setRequestMethod("GET");
                conn.connect();
            }



            // Starts the query

            int responseCode = conn.getResponseCode();
            Log.i(TAG, "from " + myurl + ", The response code from SERVER is: " + responseCode);

            is = conn.getInputStream();

            // Convert the InputStream into a string
            // i guess we look up how to do this





            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                while ((line = br.readLine()) != null) {
                    response += line;
                }
            } else {
                response = "from downloadUrl, php page response was not OK: " + responseCode;

            }

            // it's good to close these things?
            is.close();
            conn.disconnect();


            Log.i(TAG, "response is " + response);

            return response;

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

尝试使用以下代码块发送 POST 请求的参数。

Map<String,String> params = new LinkedHashMap<>();
params.put("phone", "phone");

StringBuilder postPraamString = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
     if (postPraamString.length() != 0) postPraamString.append('&');
         postPraamString.append(URLEncoder.encode(param.getKey(), "UTF-8"));
         postPraamString.append('=');
         postPraamString.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
     }
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
writer.write(postDataBytes);

所以我找到了问题的根源...

行中:

String postParams = "?phone=" + phone;

问题出在前导问号上。问号只能在 GET 请求中使用。