Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9176703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:10:01+00:00 2026-06-17T17:10:01+00:00

I am trying to refactor (to take advantage of Guava) some code that sends

  • 0

I am trying to refactor (to take advantage of Guava) some code that sends a POST request to a web service and reads the reply to a string.

Currently my code looks like that:

    HttpURLConnection conn = null;
    OutputStream out = null;

    try {
        // Build the POST data (a JSON object with the WS params)
        JSONObject wsArgs = new JSONObject();
        wsArgs.put("param1", "value1");
        wsArgs.put("param2", "value2");
        String postData = wsArgs.toString();

        // Setup a URL connection
        URL address = new URL(Constants.WS_URL);
        conn = (HttpURLConnection) address.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(false);
        conn.setDoOutput(true);
        conn.setConnectTimeout(Constants.DEFAULT_HTTP_TIMEOUT);

        // Send the request
        out = conn.getOutputStream();
        out.write(postData.getBytes());
        out.close();

        // Get the response
        int responseCode = conn.getResponseCode();
        if (responseCode != 200) {
            Log.e("", "Error - HTTP response code: " + responseCode);
            return Boolean.FALSE;
        } else {
            // Read conn.getInputStream() here
        }
    } catch (JSONException e) {
        Log.e("", "SendGcmId - Failed to build the WebService arguments", e);
        return Boolean.FALSE;
    } catch (IOException e) {
        Log.e("", "SendGcmId - Failed to call the WebService", e);
        return Boolean.FALSE;
    } finally {
        if (conn != null) conn.disconnect();

                    // Any guava equivalent here too?
        IOUtils.closeQuietly(out);
    }

I would like to understand how to use properly Guava’s InputSupplier and OutputSupplier here to get rid of quite a bunch of code. There are quite a few examples out here with files but I can’t get a nice concise version of the above code (would like to see what a Guava experienced user would do with that).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:10:02+00:00Added an answer on June 17, 2026 at 5:10 pm

    The majority of the simplification you’ll be able to get — such as it is — would be to replace the lines out = conn.getOutputStream(); out.write(postData.getBytes()); out.close(), and the need to close out yourself, with

    new ByteSink() {
      public OutputStream openStream() throws IOException {
        return conn.getOutputStream();
      }
    }.write(postData.getBytes());
    

    That opens the output stream, writes the bytes, and closes the output stream properly (as opposed to closeQuietly).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to refactor some code here that was done previously by other guys,
I am trying to refactor some code I wrote that uses IndexedDb. Ideally what
Trying to refactor some code that has gotten really slow recently and I came
I am trying to refactor some delphi 7 code. One of the procedures I'd
I have some mutable scala code that I am trying to rewrite in a
I'm trying to refactor some code but I'm kinda confused. I define my database
I am trying to refactor my code so that it follows good OO pattern,
I'm trying to refactor some code here, to make it more manageable and reader-friendly.
I'm trying to refactor some legacy code to use Spring to handle the jms
I'm trying to refactor my code to avoid DRY and found that I'm doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.