我正在尝试在 java 小程序中运行此代码:
package test;
import java.applet.Applet;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class DrawExample extends Applet {
public void paint(Graphics g) {
try {
g.drawString("CODE:",50, 30);
URL yahoo = new URL("http://www.yahoo.com/");
URLConnection yc = yahoo.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
int i=65;
while ((inputLine = in.readLine()) != null) {
g.drawString(inputLine,50, i);
i=i+15;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我从 Eclipse 将此代码作为小程序运行,它运行良好,但是如果我尝试将它嵌入网页中运行,我只会得到“CODE:”。代码冻结在这一行:
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
有人知道我在做什么错吗?谢谢!