Search code examples
javaandroidjsonhttpurlconnection

How to build the body of a request in json format


Here is the request I should call: listing library contents

GET https://photoslibrary.googleapis.com/v1/mediaItems Content-type: application/json Authorization: Bearer oauth2-token { "pageSize": "100", }

Here's what I tried:

public String getJSON(String url, int timeout) {
        String body1 = "{pageSize: 100,}";
        String body2 = "{\"pageSize\": \"100\",}";
        HttpURLConnection request = null;
        try {
            URL u = new URL(url);
            request = (HttpURLConnection) u.openConnection();

            request.setRequestMethod("GET");
            request.setRequestProperty("Authorization", "Bearer " + token);
            request.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            request.setUseCaches(false);
            request.setAllowUserInteraction(false);
            request.setConnectTimeout(timeout);
            request.setReadTimeout(timeout);
            request.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body2.getBytes().length));
            OutputStream outputStream = request.getOutputStream();
            outputStream.write(body2.getBytes());
            outputStream.close();
            request.connect();
            int status = request.getResponseCode();

            switch (status) {
                case 200:
                case 201:
                    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line+"\n");
                    }
                    br.close();
                    return sb.toString();
            }

        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (request != null) {
                try {
                    request.disconnect();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return null;
    }

If I use the GET method I get an error:

java.net.ProtocolException: method does not support a request body: GET

I tried with POST but without success.

Thanks for your help.


Solution

  • I think you need to send a param along with this request

    You can try this request in postman using this query

    https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100

    and pass the token in the authentication section.

    Here you can do something like -

    eg 
    URI uri = new URIBuilder("https://photoslibrary.googleapis.com/v1/mediaItems")
              .addParameter("pageSize", 100)
              .build();
    

    Suggestion - Use retrofit for network requests and jackson-databind project for JSON conversions.