1

我的朋友让我在这里发布他的问题,它是:

我有一个问题......我的代码运行得很好,突然出现了一个错误。我不知道它有什么问题,也许你们可以告诉我一些我正在犯的明显错误。

那是错误通信:

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

}
4

2 回答 2

4

从外观上看,该应用程序执行以下操作:

  • 检索特定网页的内容
  • 使用正则表达式从上述网页中检索特定数据
  • 提取的项目之一是图像的 URL
  • 该 URL 永远不会被验证为有效
  • 有时 URL 无效并抛出 MalformedURLException

由于抓取的网页在应用程序外部,因此无法保证提取的 URL 存在、有效或与上周相同(无论正则表达式编写得如何)。只有一个解决方案,那就是优雅地失败(可能在这些情况下显示另一张图像)并可选地通知应用程序维护人员更新用于提取信息的正则表达式和/或网页。

于 2012-05-05T19:59:50.410 回答
2

我同意@erikxiv (+1) 但想补充一点。首先,图像 URL 不一定是绝对的,即看起来像一个完整的 URL,例如http://host/path/img.jpg. 它可以是相对于页面底部定义的相对引用,例如images/theimage.jpg. 您必须检测这种情况(例如使用正则表达式)并在调用构造函数之前将完整的 URL 规范创建为字符串URL

请不要将其视为个人信息,但您的代码包含许多用于字符串解析的硬编码常量。即使它现在对您有用,它也非常脆弱,并且每次都可能被破坏和失败weather.com,即使在他们的网站上进行很小的更改。我建议您使用正则表达式,或者更好地使用用 java 实现的广泛可用的 HTML 解析器。

最后的评论。您在代码中使用波兰语名称。我相信这会让不懂波兰语的人很难理解你的代码。就我所知的俄语而言,这并没有让我太困扰,但这里的大多数人都没有这个特权。

无论如何,祝你发展和学习好运。

于 2012-05-05T20:17:42.040 回答