What exactly does URLConnection.setDoOutput() affect?

There’s setDoOutput() in URLConnection. According to documentation I should

Set the DoOutput flag to true if you intend to use the URL connection for output, false if not.

  • Setting Button text font in android
  • android locale randomly changes back to default
  • When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?
  • Nsd Manager Android Crashing on 4.2.2
  • Android Ant project reference
  • accidentally removed android dependencies folder
  • Now I’m facing exactly this problem – the Java runtime converts the request to POST once setDoOutput(true) is called and the server only responds to GET requests. I want to understand what happens if I remove that setDoOutput(true) from the code.

    What exactly will this affect? Suppose I set it to false – what can I do now and what can’t I do now? Will I be able to perform GET requests? What is “output” in context of this method?

    Related posts:

    Views above and below RecyclerView
    How to reduce the gap between navigation icon and toolbar title?
    Cannot import google cloud endpoints client library class in Android project
    Asynctask Error Handling
    google api client callback is never called
    Flurry Agent causing a CalledFromWrongThreadException exception on Android 2.3.4
  • How to create custom palette with custom color for Material Design App?
  • How to disable interaction with html5 videos in webview or properly catch their exceptions?
  • Use an intent to send data to my activity
  • Sound recognition in Android
  • How to examine SharedPreferences from adb shell?
  • Android EditText listener for cursor position change
  • 4 Solutions collect form web for “What exactly does URLConnection.setDoOutput() affect?”

    You need to set it to true if you want to send (output) a request body, for example with POST or PUT requests. With GET, you do not usually send a body, so you do not need it.

    Sending the request body itself is done via the connection’s output stream:

    conn.getOutputStream().write(someBytes);
    

    setDoOutput(true) is used for POST and PUT requests. If it is false then it is for using GET requests.

    Adding a comment, if you have a long lasting connection and you send both GETs and POSTs, this is what I do:

    if (doGet) {    // some boolean
        con.setDoOutput(false); // reset any previous setting, if con is long lasting
        con.setRequestMethod("GET");
    }
    else {
        con.setDoOutput(true);  // reset any previous setting, if con is long lasting
        con.setRequestMethod("POST");
    }
    

    And to avoid making the connection long lasting, close it each time.

    if (doClose)    // some boolean
        con.setRequestProperty("Connection", "close");
    
    con.connect();              // force connect request
    
    public void setDoOutput( boolean dooutput )
    

    It takes a value as the parameter and sets this value of the doOutput field for this URLConnection to the specified value.

    A URL connection can be used for input and/or output. Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.