-2

だから、私は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 からムービー データを出力するプログラムであり、問​​題が発生しています。ムービーを入力して Enter キーを押すと、まだ 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!";
    }
}

Test-class を書き直しました。main-method の名前が変更され、return ステートメント (文字列) が追加され、while ループが削除されました。1 つのプロジェクトで複数の main(String[] args) メソッドを使用しないようにしてください。そうしないと、コンテキストが切り替わった場合に、IDE が「間違った」メソッドを起動する可能性があります。私はそれをテストしませんでしたが、これで、GUI クラスから Test.getMovieInfo("movie-name") を呼び出して情報を取得できるはずです。(それでも、このコードはリファクタリングする必要があります;))

于 2013-09-16T19:53:50.530 に答える