Java 从http请求获取新数据后更新gui
我想建立一个小工具来监控我最喜欢的Let's Player Gronkh 到目前为止,我得到的是: 主要类别:Java 从http请求获取新数据后更新gui,java,user-interface,http-request,Java,User Interface,Http Request,我想建立一个小工具来监控我最喜欢的Let's Player Gronkh 到目前为止,我得到的是: 主要类别: public class mainClass { public static void main(final String[] args) throws Exception { // final String url = "https://api.twitch.tv/kraken/streams/gronkh/"; // final worke
public class mainClass {
public static void main(final String[] args) throws Exception {
// final String url = "https://api.twitch.tv/kraken/streams/gronkh/";
// final workerClass w = new workerClass(url);
gronkhGui.launch();
// String URL = "http://eu.battle.net/api/d3/profile/tortoc-2624/";
// final String bNetString = d3AccInfo.request.sendGet(URL);
// System.out.println(bNetString);
}
}
图形用户界面:
public class gronkhGui extends JFrame {
public gronkhGui() {
getContentPane().setLayout(
new FormLayout(new ColumnSpec[] {
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC, }));
final JLabel textLabel = new JLabel("Gronkh ist: ");
textLabel.setFont(new Font("Calibri", Font.PLAIN, 14));
getContentPane().add(textLabel, "6, 2");
final JLabel stateLabel = new JLabel("");
stateLabel.setFont(new Font("Calibri", Font.PLAIN, 30));
getContentPane().add(stateLabel, "6, 6");
final JLabel gameLabel = new JLabel("");
getContentPane().add(gameLabel, "6, 10");
final JLabel gameLabelText = new JLabel("");
getContentPane().add(gameLabelText, "6, 12");
final JLabel viewerLabel = new JLabel("");
getContentPane().add(viewerLabel, "6, 16");
final JLabel viewerLabelText = new JLabel("");
getContentPane().add(viewerLabelText, "6, 18");
final String url = "https://api.twitch.tv/kraken/streams/gronkh/";
final workerClass w = new workerClass(url);
if (w.state.equals("OFFLINE")) {
stateLabel.setText(w.state);
gameLabel.setText("");
gameLabelText.setText("");
viewerLabel.setText("");
viewerLabelText.setText("");
getContentPane().setBackground(Color.red);
} else {
stateLabel.setText(w.state);
gameLabel.setText("Gronkh spielt:");
gameLabelText.setText(w.game);
viewerLabel.setText("Zuschauer:");
viewerLabelText.setText(w.viewers);
getContentPane().setBackground(Color.green);
}
}
public static void launch() {
final gronkhGui gui = new gronkhGui();
gui.setVisible(true);
gui.setBounds(400, 200, 170, 304);
}
}
工人阶级:
public class workerClass {
public String state;
public String game;
public String viewers;
public String name;
public workerClass(final String url) {
String request = "";
try {
request = gronkhInfo.request.sendGet(url);
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!request.contains("\"stream\":null")) {
this.state = "ONLINE";
this.game = request.substring(request.indexOf("game\" : \"") + 8,
request.indexOf("\",\"viewers"));
this.viewers = request.substring(
request.indexOf("viewers\": ") + 10,
request.indexOf(",\"created_at"));
this.name = request.substring(
request.indexOf("display_name\": \"") + 16,
request.indexOf("\",\"game"));
} else {
this.state = "OFFLINE";
}
}
}
请求类:
public class request {
public final static String USER_AGENT = "Mozilla/5.0";
public static String sendGet(final String url) throws Exception {
final URL obj = new URL(url);
final HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
final BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
final String respo = response.toString();
return respo;
}
}
我现在想做的是更新Gui,例如每30秒更新一次。
这意味着我必须再次执行http请求,然后更新gui
如何执行此操作?您可以将workerClass定义为可运行线程,并可以使用ScheduledThreadPool执行器每30秒运行一次,如:
ScheduledExecutorService sevice = Executors.newScheduledThreadPool(1);
sevice.schedule(new workerClass(), 30, TimeUnit.SECONDS);
然后,您可以用workerClass创建的新字符串通知GUI。我已经插入了代码,并使我的类abstract并实现了Runnable。现在,代码中的“new workerClass()”显示了一个错误:“无法实例化类型workerClass”,我如何通知gui?