Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Tolls

Thread: Is HttpURLConnection.connect() method redundant?

  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Is HttpURLConnection.connect() method redundant?

    Hi,

    In an Asynctask class and its doInBackground(String... params) method, the following code is used to download an Image in the background thread:

    Java Code:
           URL url = new URL(params[0]);
           HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    //     connection.connect();
           InputStream inputStream = connection.getInputStream();
           Bitmap img = BitmapFactory.decodeStream(inputStream);
           return img;
    But the point is the commented line (connection.connect();) makes no change whether would be there or not. Isn't it necessary for the created connection to connect()?
    Or it works like the connection just needs to exist, then it gets input streams straightforward without the need to connect separately?

    Thanks
    Last edited by ghandil; 08-01-2016 at 11:53 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Is HttpURLConnection.connect() method redundant?

    This is what the API documentation has to say about it:

    1: The connection object is created by invoking the openConnection method on a URL.
    2: The setup parameters and general request properties are manipulated.
    3: The actual connection to the remote object is made, using the connect method.
    4: The remote object becomes available. The header fields and the contents of the remote object can be accessed.
    Step 1) and 2) need to be performed before you can actually open the object in step 3); that's why the explicit method is present.

    kind regards,

    Jos
    ghandil likes this.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: Is HttpURLConnection.connect() method redundant?

    Thank you JosAH,

    I didn't get it quite.
    So the first two step are required for functionality, but the third step is not necessary as they are? What does happen if connect() would be ignored?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Is HttpURLConnection.connect() method redundant?

    From the JavaDoc for connect():
    "
    Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.
    "
    I suspect that getInputStream is one such operation.
    ghandil likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: Is HttpURLConnection.connect() method redundant?

    Quote Originally Posted by Tolls View Post
    "
    Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.
    "
    I suspect that getInputStream is one such operation.
    Ah Interesting, Thanks Tolls

Similar Threads

  1. Replies: 6
    Last Post: 05-21-2015, 11:19 AM
  2. Replies: 4
    Last Post: 06-14-2013, 10:10 PM
  3. Replies: 2
    Last Post: 11-15-2012, 04:20 PM
  4. connect to http URL and abstract method
    By axa in forum Networking
    Replies: 1
    Last Post: 09-05-2011, 12:40 PM
  5. C# HttpURLConnection to java
    By jmorr212 in forum Networking
    Replies: 0
    Last Post: 02-24-2011, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •