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

    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 {
    		setTitle(address);
    		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:
    connect()
    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
    Posts
    12,009
    Rep Power
    20

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