Results 1 to 2 of 2
  1. #1
    axa is offline Member
    Join Date
    Sep 2011
    Rep Power

    Default connect to http URL and abstract method

    Hallo everybody ;)

    My first question is not about something that is not working but more something that I don't fully understand. Here is the code of what I'm doing:

    Java Code:
    void getData(String address) throws MalformedURLException {
    		URL page = new URL("address");			// Create a URL attached to "address" (ftp, http, ssl ) 
    		StringBuffer text = new StringBuffer();
    		try {
    			URLConnection conn;					// Create a generic variable referencing a URLConnection
    			conn = page.openConnection();		// Assign to the conn variable the URLConnection object associated with the URL specified before
    			conn = (HttpURLConnection) conn;	// Cast the generic URLConnection in a HttpURLConnection
    			conn.connect(); // ?? From which class does this method's implementation come from?
    			// rest of the code.....
    		} catch (IOException e) {
    			System.out.println("Error - " + e.toString());
    I checked the documentation and the classes I'm using are:
    URL - public class
    URLConnection - public abstract class
    HttpURLConnection - public abstract class

    the method
    Java Code:
    that I'm calling on the conn object it is inherited by HttpURLConnection from the URLConnection class, but there the method is also abstract:
    Java Code:
    public abstract void connect()
                          throws IOException
    I was wondering which class is providing the implementation of this method in this case.
    Thanks ;-)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: connect to http URL and abstract method

    Java Code:
    conn = (HttpURLConnection) conn;
    That cast is completely pointless. You're not changing anything about conn, if that's what you think you;re doing. It's still a URLConnection.

    As for what class comes back from openConnection(), it's whatever class the URL class used considers suitable (depending I think on the actual URL used). You don't actually have to know what it is, other than it is a URLConnection of some sort. Though, in your case, you can presume it is an HttpURLConnection of some sort...if you need access to the extra bits that provides.

Similar Threads

  1. Replies: 4
    Last Post: 04-14-2011, 12:42 PM
  2. Replies: 4
    Last Post: 02-19-2011, 05:32 AM
  3. Replies: 3
    Last Post: 08-13-2010, 05:57 AM
  4. Replies: 3
    Last Post: 09-16-2009, 10:27 PM
  5. Replies: 2
    Last Post: 11-21-2008, 01:20 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