所以,我有两节课。 主要课程:
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 class:
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中输出电影数据,而我遇到了一个问题。 当我输入电影,然后按回车键时,它仍然显示为空,似乎没有从我正在使用的API输出数据。
答案 0 :(得分: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方法,添加return-statements(String)并删除while循环。尝试避免在一个项目中使用多个main(String [] args)方法,否则如果您的上下文切换,您的IDE可能会启动“错误”的方法。 我没有测试它,但是现在你应该可以从gui-class调用Test.getMovieInfo(“movie-name”)来获取信息。 (尽管如此,这段代码应该重构;))