2012-05-05 58 views
1

我的朋友問我在這裏發表他的問題,它就在那裏:誤差MalformedURLException的

我有一個問題,這個...我的代碼工作只是罰款,以及突然出現的錯誤。我不知道它有什麼不對,也許你們可以告訴我一些明顯的錯誤。

這是錯誤傳達:

> Exception in thread "main" java.net.MalformedURLException: no 
> protocol: 
>   at java.net.URL.<init>(URL.java:583) 
>   at java.net.URL.<init>(URL.java:480) 
>   at java.net.URL.<init>(URL.java:429) 
>   at pogoda.Okienko.getJPanel(Okienko.java:120) 
>   at pogoda.Okienko.getJContentPane(Okienko.java:101) 
>   at pogoda.Okienko.initialize(Okienko.java:59) 
>   at pogoda.Okienko.<init>(Okienko.java:49) 
>   at pogoda.Pogoda.main(Pogoda.java:119) 

,這是我的代碼:

package pogoda; 

import javax.swing.ImageIcon; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import java.awt.Rectangle; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionListener; 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 


public final class Okienko extends JFrame 
{ 

     public static final long serialVersionUID = 1L; 
     public JPanel jContentPane = null; 
     public JLabel jLabel = null; 
     public JLabel jLabel1 = null; 
     public JLabel LabelTemp = null; 
     public JLabel LabelOdczuwalna = null; 
     public JPanel jPanel = null; 

     public Okienko(double temperatura, double odczuwalna, String adres) throws MalformedURLException 
     { 
       super(); 
       initialize(temperatura, odczuwalna, adres); 
     } 

public void initialize(double temperatura, double odczuwalna, String adres) throws MalformedURLException 
     { 
       this.setLocation(350, 250); 
       this.setSize(350, 350); 
       this.setContentPane(getJContentPane(temperatura, odczuwalna,adres)); 
       this.setTitle("Pogoda"); 
       this.setResizable(false); // jesli damy na true bedziemy mogli zmienic rozmiar naszego okienka 
       this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

     } 

public JPanel getJContentPane(double temperatura, double odczuwalna, String adres) throws MalformedURLException 
     { 
       if (jContentPane == null) 
       { 
         LabelOdczuwalna = new JLabel(); 
         LabelOdczuwalna.setBounds(new Rectangle(200, 50, 100, 20)); 
         odczuwalna *=100; 
         int pomoc=(int) odczuwalna; 
         odczuwalna = ((double)pomoc)/100; 
         LabelOdczuwalna.setText("" + odczuwalna + " °C"); 
         LabelTemp = new JLabel(); 
         LabelTemp.setBounds(new Rectangle(200, 10, 100, 20)); 
         temperatura *=100; 
         pomoc = (int)temperatura; 
         temperatura = ((double)pomoc)/100; 
         LabelTemp.setText("" + temperatura + " °C"); 
         jLabel1 = new JLabel(); 
         jLabel1.setBounds(new Rectangle(5, 50, 300, 20)); 
         jLabel1.setText("Temperatura odczuwalna"); 
         jLabel = new JLabel(); 
         jLabel.setBounds(new Rectangle(5, 10, 300, 20)); 

         jLabel.setText(" Aktualna temperatura "); 
         jContentPane = new JPanel(); 
         jContentPane.setLayout(null); 
         jContentPane.add(jLabel, null); 
         jContentPane.add(jLabel1, null); 
         jContentPane.add(LabelTemp, null); 
         jContentPane.add(LabelOdczuwalna, null); 
         jContentPane.add(getJPanel(adres), null); 
       } 
       return jContentPane; 
     } 

public JPanel getJPanel(String adres) throws MalformedURLException 
{ 
    if (jPanel == null) 
    { 
     jPanel = new JPanel(); 
     jPanel.setLayout(new GridBagLayout()); 
     jPanel.setBounds(new Rectangle(75, 100, 150, 150)); 
    } 

    java.net.URL where = new URL(adres); 
    ImageIcon icon = new ImageIcon(where); 
    JLabel pomocnik = new JLabel(); 
    pomocnik.setIcon(icon); 
    jPanel.add(pomocnik); 
    return jPanel; 
} 

}

package pogoda; 



/** 
* 
* @author mateusz 
*/ 

import com.sun.corba.se.impl.logging.OMGSystemException; 
import java.io.*; 
import java.net.MalformedURLException; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 






import java.net.*; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
import java.io.*; 


public class Pogoda 
{ 


     public int counter = 1; 
     public static String linia1 = 
       "<span class=\"twc-forecast-when twc-none\">Right Now</span>"; 
     public static String linia11 = 
       "<img src=\""+ 
       ".+"+ 
       "\" alt="; 

     public static String linia2 = 
       "<td class=\"twc-col-1 twc-forecast-temperature\"><strong>"+ 
       "\\d+"+ 
       "&deg;F</strong></td>"; 
     public static String linia3 = 
       "<td class=\"twc-col-1 twc-forecast-temperature-info\">Feels Like: <strong>"+ 
       "\\d+"+ 
       "&deg;</strong>"; 




     public static void main(String args[]) throws IOException 
     { 

       int temperatura =0; 
       int odczuwalna =0; 
       String adresObrazka=""; 
       String help = ""; 
       String linia = ""; 
       URL strona = new URL("http://www.weather.com/weather/today/Wroclaw+Poland+PLXX0029"); 
       URLConnection con = (URLConnection) strona.openConnection(); 

       con.connect(); 
       BufferedReader in = new BufferedReader(new InputStreamReader(strona.openStream())); 
       while((linia = in.readLine()) != null) 
       { 
           Pattern p = Pattern.compile(linia2); 
           Matcher m = p.matcher(linia); 
           if(m.find()) 
           { 

             help = m.group().substring(55,m.group().length()-20); 
             temperatura = Integer.parseInt(help); 
             System.out.println(temperatura); 

           } 
           p = Pattern.compile(linia3); 
           m = p.matcher(linia); 
           if(m.find()) 
           { 
             System.out.println("znaleziono odczuwalna"); 
             help = m.group().substring(72,m.group().length()-14); 
             System.out.println(help); 
             odczuwalna=Integer.parseInt(help); 
           } 
           p = Pattern.compile(linia1); 
           m = p.matcher(linia); 
           if(m.find()) 
           { 
             linia = in.readLine(); 
             p = Pattern.compile(linia11); 
             m = p.matcher(linia); 
             if(m.find()) 
             { 
               System.out.println("Pobrałem adres obrazka"); 
               System.out.println(m.group().substring(10,m.group().length()-6)); 
               adresObrazka = m.group().substring(10,m.group().length()-6); 
             } 
           } 


       } 

       double temperatura1 = temperatura; 
       double odczuwalna1 = odczuwalna; 
       odczuwalna1 = ((odczuwalna1-32)*5)/9; 
       temperatura1 = ((temperatura1-32)*5)/9; 
       System.out.println("Dane pobrane"); 
       System.out.println("Temperatura " + temperatura1 + "\n Odczuwalna " + odczuwalna1 + "\n Adres obrazka " + adresObrazka); 
       Okienko o = new Okienko(temperatura1,odczuwalna1,adresObrazka); 
       o.setVisible(true); 
       o.jContentPane.setVisible(true); 
     } 

} 
+0

'Okienko o = new Okienko(temperatura1,odczuwalna1,adresObrazka)''adresObrazka'傳遞的值是多少? – assylias

+0

adresObrazka = m.group()。substring(10,m.group()。length() - 6) –

+0

謝謝,我可以看到。我的意思是:那個字符串的價值是什麼,這顯然是你錯誤的根源? – assylias

回答

4

從它的應用程序的外觀執行以下操作:

  • 檢索特定網頁的內容
  • 使用正則表達式從上述網頁
  • 一箇中提取的項目的檢索特定的數據是一個URL來
  • 的URL是從來沒有驗證的圖像有效
  • 偶爾的URL無效,一個MalformedURLException異常被拋出

由於刮網頁是外部應用程序也無法保證提取的URL存在,有效,或與上一週相同(不管正則表達式寫得多好)。只有一個解決方案,那就是優雅地失敗(可能在這些情況下顯示另一個圖像),並可選擇通知應用程序維護人員更新用於提取信息的正則表達式和/或網頁。

2

我同意@erikxiv(+1),但想添加一些東西。首先,圖像URL不一定是絕對的,即看起來像完整的URL,例如http://host/path/img.jpg。它可以是相對於頁面底部定義的相對參考,例如,像images/theimage.jpg。你必須檢測這種情況(例如使用正則表達式),並在調用構造函數URL之前創建完整的URL規範。

請不要把它當作個人,但是你的代碼包含很多用於字符串解析的硬編碼常量。即使它現在對你有用,它也非常脆弱,可以被破壞,並且每次weather.com都會在其網站上發生非常小的變化。我建議你使用正則表達式,或者更好地使用在java中實現的廣泛可用的HTML解析器。

而最後的評論。您在代碼中使用波蘭語的名稱。我相信這會讓你的代碼很難理解不懂波蘭語的人。就我所知俄語而言,它並不會讓我感到困擾,但大多數這裏的人都沒有這種特權。

無論如何,祝你好運在你的發展和研究。