2016-07-29 23 views
2

Ich versuche eine http-Verbindung herzustellen und den Inhalt einer HTML-Seite zu lesen. Dies ist die Klasse I auf eine Seite verbinden bin mit,Ich kann keine Webseite verbinden und lesen

public class Connection { 

    private URL url; 
    private HttpURLConnection httpURLConnection; 


    public Connection(String url) throws MalformedURLException { 

     this.url = new URL(url); 
     this.httpURLConnection = new HttpURLConnection(this.url) { 
      @Override 
      public void connect() throws IOException { 

       httpURLConnection.connect(); 
       httpURLConnection.setReadTimeout(15000); 
       httpURLConnection.setConnectTimeout(15000); 
       httpURLConnection.setUseCaches(false); 
       HttpURLConnection.setFollowRedirects(true); 

      } 

      @Override 
      public void disconnect() { 
       httpURLConnection.disconnect(); 
      } 

      @Override 
      public boolean usingProxy() { 
       return false; 
      } 
     }; 
    } 

    public String parse() throws IOException { 
     InputStream is = httpURLConnection.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while ((line = rd.readLine()) != null) 
     { 
      response.append(line); 
      response.append('\n'); 
     } 
     rd.close(); 
     return response.toString(); 
    } 
} 

Das ist die Aufruf-Code,

public static void main(String[] args) { 
    // write your code here 
     String url = "https://www.buffalo.edu"; 
     Connection c; 
     String str = null; 
     try { 
      c = new Connection(url); 
      str = c.parse(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     System.out.println(str); 
    } 

ich die folgende Ausnahme erhalten

null 
java.net.UnknownServiceException: protocol doesn't support input 
    at java.net.URLConnection.getInputStream(URLConnection.java:830) 
    at io.soumasish.connections.Connection.parse(Connection.java:47) 
    at io.soumasish.App.main(App.java:17) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

Was soll ich tun falsch hier. Jede Hilfe wird geschätzt.

Antwort

1

Sie haben eine falsche Verwendung von HttpURLConnection. Es ist eine Schleife von connect(), die nichts tut

this.httpURLConnection = new HttpURLConnection(this.url) { 
    @Override 
    public void connect() throws IOException { 
     httpURLConnection.connect(); 
     ... 

Die richtige Muster der HttpURLConnection Klasse hier erwähnt wird: https://developer.xamarin.com/api/type/Java.Net.HttpURLConnection/

Ändern Sie bitte Ihre Connection() wie unten wird es funktioniert.

public Connection(String url) throws IOException { 
    this.url = new URL(url); 
    this.httpURLConnection = (HttpURLConnection) this.url.openConnection(); 
} 
Verwandte Themen