2015-03-08 1 views
0

Je veux créer un petit outil pour surveiller mon favori Let's Player Gronkh.Mise à jour d'une interface graphique après avoir reçu de nouvelles données de la requête http

ce que j'ai jusqu'à présent:

Une classe principale:

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); 

    } 
} 

A Gui:

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); 
    } 

} 

Une classe ouvrière:

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"; 
     } 
    } 
} 

Une classe de demande:

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; 

    } 
} 

Ce que je veux maintenant faire est de mettre à jour le Gui par exemple toutes les 30 secondes. Cela signifie que je dois à nouveau faire une requête http, puis renouveler l'interface graphique.

Comment puis-je faire cela?

+0

Retrait fixe. –

Répondre

0

Vous pouvez définir workerClass comme fil Runnable et vous pourriez exécuter toutes les 30 secondes en utilisant comme exécuteur ScheduledThreadPool:

ScheduledExecutorService sevice = Executors.newScheduledThreadPool(1); 
sevice.schedule(new workerClass(), 30, TimeUnit.SECONDS); 

Et vous pourriez dire avec GUI informer nouvelle chaîne créée de workerClass.

+0

J'ai inséré le code et fait ma classe absract et implémenté Runnable. Maintenant, le "nouveau workerClass()" hors de vous le code affiche une erreur disant: "Impossible d'instancier le type workerClass" Comment puis-je informer le GUI? – tortoc