Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package menu; import com.squareup.okhttp.FormEncodingBuilder; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; /** * * @author jossie */ public class pila extends javax.swing.JFrame { public static OkHttpClient webClient2 = new OkHttpClient(); /** * Creates new form pila */ public pila() { initComponents(); this.setDefaultCloseOperation(pila.DISPOSE_ON_CLOSE); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("push"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("pop"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(37, 37, 37).addComponent( jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(96, 96, 96).addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addContainerGap(59, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(48, 48, 48) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2).addContainerGap(41, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String val = jTextField1.getText().toString(); RequestBody formBody = new FormEncodingBuilder().add("valor", val).build(); String q = getString("AddPila", formBody); System.out.println(q + "#"); // TODO add your handling code here: }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed String val = jTextField1.getText().toString(); RequestBody formBody2 = new FormEncodingBuilder().add("valor", val).build(); String w = getString2("PopPila", formBody2); System.out.println(w + "#"); // TODO add your handling code here: }//GEN-LAST:event_jButton2ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(pila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(pila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(pila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(pila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new pila().setVisible(true); } }); } public static String getString(String metodo, RequestBody formBody) { try { URL url = new URL("http://0.0.0.0:5000/" + metodo); Request request = new Request.Builder().url(url).post(formBody).build(); Response response = webClient2.newCall(request).execute();//Aqui obtiene la respuesta en dado caso si hayas pues un return en python String response_string = response.body().string();//y este seria el string de las respuesta return response_string; } catch (MalformedURLException ex) { java.util.logging.Logger.getLogger(menu.cola.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { java.util.logging.Logger.getLogger(menu.cola.class.getName()).log(Level.SEVERE, null, ex); } return null; } public static String getString2(String metodo, RequestBody formBody) { try { URL url = new URL("http://0.0.0.0:5000/" + metodo); Request request = new Request.Builder().url(url).post(formBody).build(); Response response = webClient2.newCall(request).execute();//Aqui obtiene la respuesta en dado caso si hayas pues un return en python String response_string = response.body().string();//y este seria el string de las respuesta return response_string; } catch (MalformedURLException ex) { java.util.logging.Logger.getLogger(menu.pila.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { java.util.logging.Logger.getLogger(menu.pila.class.getName()).log(Level.SEVERE, null, ex); } return null; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables }