2016-07-29 21 views
2

http接続を確立し、HTMLページの内容を読み取ろうとしています。これは私がページに接続するために使用しているクラスで、Webページに接続できません。

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(); 
    } 
} 

これは、呼び出し元のコードで、

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); 
    } 

私は次の例外

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) 

私は何をやっているの取得しますここで間違っている。どんな助けもありがたい。

答えて

1

もここで参照されています。 https://developer.xamarin.com/api/type/Java.Net.HttpURLConnection/

を以下のようなあなたのConnection()はそれを動作させるだろう変更していない:それは何も

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

のHttpURLConnectionクラスの正しいパターンがここで言及されてはないconnect()のループです。

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

このスレッドでは、なぜこれが失敗しているのかについて、同じ問題がよく説明されています(Creating HttpURLConnection for URLStreamHandlerFactory, getting Error: protocol doesn't support input)。

本は、あなたがHttpURLConnectionの間違った使用法を持っている(https://books.google.de/books?id=LXsgAQAAQBAJ&lpg=PT285&ots=H-snX4lzGe&dq=Java+should+I+subclass+URLStreamHandler&pg=PT286&redir_esc=y#v=snippet&q=connect()&f=false

関連する問題