如何在Java中设置GET请求中的参数

时间:2013-08-09 15:53:23

标签: java android get

所以我想发送带参数的GET请求。但它似乎只有您发送请求的URL的约定。与POST请求不同,我认为无法在其中传递参数。

我现在如何发送GET请求,没有参数(可能是错误的):

String url = "http://api.netatmo.net/api/getuser";

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // optional default is GET
            con.setRequestMethod("GET");

            //add request header
            con.setRequestProperty("User-Agent", USER_AGENT);

            int responseCode = con.getResponseCode();
            Log.v(TAG, ("\nSending 'GET' request to URL : " + url));
            Log.v(TAG, ("Response Code : " + responseCode));

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            Log.v(TAG, (response.toString()));

我如何使用参数发送POST请求:

String url = "https://api.netatmo.net/oauth2/token";
            URL obj = new URL(url);
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

            //add request header
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

            String urlParameters = "grant_type=password&client_id=myid&client_secret=mysecret&username=myusername&password=mypass";

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            Log.v(TAG, "\nSending 'POST' request to URL : " + url);
            Log.v(TAG, "Post parameters : " + urlParameters);
            Log.v(TAG, "Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            Log.v(TAG, response.toString());

            access_token = response.substring(17, 74);
            refresh_token = response.substring(93,150);
            getRequest = "/api/getuser?access_token=" + access_token + " HTTP/1.1";

            Log.v(TAG, access_token);
            Log.v(TAG, refresh_token);
            Log.v(TAG, getRequest);

4 个答案:

答案 0 :(得分:9)

根据HTTP规范GET仅支持路径参数或url参数,因此您不能像在POST请求中那样将参数放在HTTP请求体中。

正如Sotirios在评论中提到的那样,从技术上讲,你仍然可以在GET体内推动params,但如果API遵守规范,它们将无法为你提供一种方法。

答案 1 :(得分:1)

您是否尝试将查询参数添加到请求java.net.URL?

String url = "http://api.netatmo.net/api/getuser?access_token=" + access_token;
URL obj = new URL(url);

答案 2 :(得分:0)

原来API提供的网址让我很困惑。我修复了网址,现在可以使用了。

答案 3 :(得分:0)

我遇到了同样的问题,试着这个:

String bla = "http://api.netatmo.net/api/devicelist?access_token=" + AUTH_TOKEN;
URL url = new URL(bla);

BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

String line = "";
String message = "";

while ((line = reader.readLine()) != null)
{
    message += line;
}

我得到一个例外,语法不正确。当我更改语法时(例如使用UTF 8进行编码),API只会返回错误(例如404找不到...)。

我终于使用它了:

try
{

        System.out.println("Access Token: " + AUTH_TOKEN);

        String url = "http://api.netatmo.net/api/devicelist";
        String query = "access_token=" + URLEncoder.encode(AUTH_TOKEN, CHARSET);

        URLConnection connection = new URL(url + "?" + query).openConnection();
        connection.setRequestProperty("Accept-Charset", CHARSET);

        InputStream response = connection.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(response));

        String line = "";
        String message = "";

        while ((line = reader.readLine()) != null)
        {
            message += line;
        }

        return message;


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

注意:CHARSET =“UTF-8”