This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
java:urls [2015/03/23 20:29] gthanos [Αναλύοντας ένα URL με βάση το API της κλάσης] |
java:urls [2015/03/23 20:53] gthanos [Χρησιμοποιώντας την κλάση URL για να ανοίξουμε ένα απομακρυσμένο resource] |
||
---|---|---|---|
Line 59: | Line 59: | ||
===== Χρησιμοποιώντας την κλάση URL για να ανοίξουμε ένα απομακρυσμένο resource ===== | ===== Χρησιμοποιώντας την κλάση URL για να ανοίξουμε ένα απομακρυσμένο resource ===== | ||
- | <code java> | + | <code java URLReader.java> |
import java.net.*; | import java.net.*; | ||
import java.io.*; | import java.io.*; | ||
public class URLReader { | public class URLReader { | ||
- | public static void main(String[] args) throws MalformedURLException { | + | public static void main(String[] args) { |
+ | URL url=null; | ||
+ | try { | ||
+ | url = new URL(args.length>0 ? args[0] : "http://feeds.bbci.co.uk/news/rss.xml"); | ||
+ | BufferedReader in = new BufferedReader( | ||
+ | new InputStreamReader(url.openStream()) ); | ||
- | URL oracle = new URL("http://www.oracle.com/"); | + | String inputLine; |
- | BufferedReader in = new BufferedReader( | + | while ((inputLine = in.readLine()) != null) { |
- | new InputStreamReader(oracle.openStream()) ); | + | System.out.println(inputLine); |
+ | } | ||
+ | in.close(); | ||
+ | } | ||
+ | catch(MalformedURLException ex) { | ||
+ | System.out.println("Malformed URL: +"+ args[0] ); | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | catch(IOException ex) { | ||
+ | System.out.println("Error while reading or writing from URL: "+url.toString() ); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
- | String inputLine; | + | To παραπάνω θα μπορούσε να γραφεί ισοδύναμα ως εξής με χρήση ενός αντικειμένου [[http://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html|URLConnection]]. |
- | while ((inputLine = in.readLine()) != null) | + | <code java URLConnectionReader.java> |
- | System.out.println(inputLine); | + | import java.net.*; |
- | in.close(); | + | import java.io.*; |
+ | |||
+ | public class URLConnectionReader { | ||
+ | public static void main(String[] args) { | ||
+ | URL url=null; | ||
+ | try { | ||
+ | url = new URL(args.length>0 ? args[0] : "http://feeds.bbci.co.uk/news/rss.xml"); | ||
+ | URLConnection urlcon = url.openConnection(); | ||
+ | BufferedReader in = new BufferedReader( | ||
+ | new InputStreamReader(urlcon.getInputStream()) ); | ||
+ | |||
+ | String inputLine; | ||
+ | while ((inputLine = in.readLine()) != null) { | ||
+ | System.out.println(inputLine); | ||
+ | } | ||
+ | in.close(); | ||
+ | } | ||
+ | catch(MalformedURLException ex) { | ||
+ | System.out.println("Malformed URL: +"+ args[0] ); | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | catch(IOException ex) { | ||
+ | System.out.println("Error while reading or writing from URL: "+url.toString() ); | ||
} | } | ||
+ | } | ||
} | } | ||
</code> | </code> | ||