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

如何在Java中设置GET请求中的参数,java,android,get,Java,Android,Get,所以我想发送一个带有参数的GET请求。但它似乎只对发送请求的url有约定。与POST请求不同,我认为没有办法在其中传递参数 我现在如何发送GET请求,没有参数(可能是错误的): 如何发送带有参数的POST请求: String url = "https://api.netatmo.net/oauth2/token"; URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnectio

所以我想发送一个带有参数的GET请求。但它似乎只对发送请求的url有约定。与POST请求不同,我认为没有办法在其中传递参数

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

如何发送带有参数的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);

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


正如Sotirios在评论中提到的,从技术上讲,您仍然可以在GET body中推送参数,但是如果API遵守规范,它们将不会为您提供这样做的方法。

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

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

原来API提供的url让我非常困惑。我修复了url,现在它可以工作了。

我遇到了同样的问题,尝试以下方法:

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;
}
我得到一个例外,语法不正确。当我改变语法(例如用UTF8编码)时,API只会返回错误(比如404NotFound…)

我终于用这个让它工作了:

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”

@SotiriosDelimanolis Point已接受,已编辑答案,欢迎进一步编辑。谢谢必须有一种方法-我正在使用的API,虽然写得很糟糕-表明有一种方法可以传递参数-否则,他们不会使用GET方法,因为传递参数是他们想要做的do@OddCore当然,您的API将提供一种在URL中发送GET参数的方法,但我不确定它是否提供了一种在消息体中推送参数的方法。@JunedAhsan您会认为它们会,对吗…哦,顺便说一句,我想我只需要URL参数,然而,当我尝试它的时候,我得到了一个畸形的异常……请参见对朱纳德答案的评论
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();
    }