-2

所以,我有两节课。主类:

package guiprojj;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import guiprojj.gui;

import javax.swing.JFrame;

@SuppressWarnings("unused")
public class Test {
    public static String movie;
    public static String line;
    public static void main(String args[]) throws IOException {
        BufferedReader rd;
        OutputStreamWriter wr;
        //Scanner s = new Scanner(System.in);
        //System.out.println("Enter input:");
        //movie = s.nextLine();
        //movie = movie.replaceAll(" ", "%20");
        while (movie != null)
        {
            try {
                URL url = new URL("http://www.imdbapi.com/?i=&t=" + movie);
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                wr = new OutputStreamWriter(conn.getOutputStream());
                wr.flush();

                // Get the response
                rd = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                line = rd.readLine();
                if (line != null) {
                    System.out.println(line);
                } else {

                    System.out.println("Sorry! That's not a valid URL.");
                }
            } catch (UnknownHostException codeyellow) {
                System.err.println("Caught UnknownHostException: " + codeyellow.getMessage());
            }
            catch (IOException e)
            {
                System.out.println("Caught IOException:" + e.getMessage());
            }

        }
    }
}

gui类:

package guiprojj;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class gui {
    public static void main(String[] args)
    {
        JFrame maingui = new JFrame("Gui");
        JPanel pangui = new JPanel();
        JButton enter = new JButton("Enter");
        JLabel movieinfo = new JLabel(Test.line);
        final JTextField movietext = new JTextField(16);
        maingui.add(pangui);
        pangui.add(movietext);
        pangui.add(enter);
        pangui.add (movieinfo);
        maingui.setVisible(true);
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();
        enter.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)  
            {
                Test.movie = movietext.getText();
                System.out.println(Test.movie);

            }
            });
        }
}

我正在编写的是一个程序,它会在您将电影数据输入框中后从 imbd 输出电影数据,但我遇到了一个问题。当我输入电影并按回车键时,它仍然显示为 null,并且似乎没有从我正在使用的 api 输出数据。

4

1 回答 1

0
public class Test {

    public static String getMovieInfo(String movie) {
        BufferedReader rd;
        OutputStreamWriter wr;
        //Scanner s = new Scanner(System.in);
        //System.out.println("Enter input:");
        //movie = s.nextLine();
        //movie = movie.replaceAll(" ", "%20");
        if (movie != null)
        {
            try {
                URL url = new URL("http://www.imdbapi.com/?i=&t=" + movie);
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                wr = new OutputStreamWriter(conn.getOutputStream());
                wr.flush();

                // Get the response
                rd = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                String line = rd.readLine();
                if (line != null) {
                   return line;
                } else {

                    return "Sorry! That's not a valid URL.";
                }
            } catch (UnknownHostException codeyellow) {
                System.err.println("Caught UnknownHostException: " + codeyellow.getMessage());
            }
            catch (IOException e)
            {
                System.out.println("Caught IOException:" + e.getMessage());
            }

        }
        else
        {
            return "passed parameter is null!";
        }

        return "an error occured, see console!";
    }
}

我已经重写了你的测试类。重命名主方法,添加返回语句(字符串)并删除 while 循环。尽量避免在一个项目中使用多个 main(String[] args) 方法,否则如果您的上下文切换,您的 IDE 可能会启动“错误”的方法。我没有测试它,但现在你应该可以从你的 gui 类中调用 Test.getMovieInfo("movie-name") 来获取信息。(尽管如此,这段代码应该被重构;))

于 2013-09-16T19:53:50.530 回答