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 lista extends javax.swing.JFrame { public static OkHttpClient webClient3 = new OkHttpClient(); /** * Creates new form lista */ public lista() { initComponents(); this.setDefaultCloseOperation(lista.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(); jTextField2 = new javax.swing.JTextField(); jButton2 = new javax.swing.JButton(); jTextField3 = new javax.swing.JTextField(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Agregar"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Borrar"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Buscar"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(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(111, 111, 111).addComponent(jButton1)) .addGroup(layout.createSequentialGroup().addGap(61, 61, 61).addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField2) .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE) .addComponent(jTextField1))) .addGroup(layout.createSequentialGroup().addGap(114, 114, 114).addComponent(jButton3)) .addGroup(layout.createSequentialGroup().addGap(112, 112, 112).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(79, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(43, 43, 43) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1).addGap(18, 18, 18) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2).addGap(18, 18, 18) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3).addContainerGap(29, 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 formBody2 = new FormEncodingBuilder().add("valor", val).build(); String w = getString1("AddLista", formBody2); System.out.println(w + "#"); // TODO add your handling code here: }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed String val = jTextField2.getText().toString(); RequestBody formBody2 = new FormEncodingBuilder().add("valor", val).build(); String w = getString2("popLista", formBody2); System.out.println(w + "#"); // TODO add your handling code here: }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed String val = jTextField3.getText().toString(); RequestBody formBody2 = new FormEncodingBuilder().add("valor", val).build(); String w = getString3("searchLista", formBody2); System.out.println(w + "#"); // TODO add your handling code here: }//GEN-LAST:event_jButton3ActionPerformed /** * @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(lista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(lista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(lista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(lista.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 lista().setVisible(true); } }); } public static String getString1(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 = webClient3.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.lista.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { java.util.logging.Logger.getLogger(menu.lista.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 = webClient3.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.lista.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { java.util.logging.Logger.getLogger(menu.lista.class.getName()).log(Level.SEVERE, null, ex); } return null; } public static String getString3(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 = webClient3.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.lista.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { java.util.logging.Logger.getLogger(menu.lista.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.JButton jButton3; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; // End of variables declaration//GEN-END:variables }