POST params空,我做错了什么? HttpURLConnection / Android / Java

时间:2016-05-03 06:53:19

标签: java android http post httpurlconnection

下面的代码显示了一个方法downloadUrl(),它接受一个String,“myurl”,它的参数。我发送的网址只有两个,而且每种方法的行为都不同。

当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();
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码块发送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);

答案 1 :(得分:0)

所以我想出了问题的根源......

在行中:

String postParams = "?phone=" + phone;

问题在于引导问号。问号只应在GET请求中使用。