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 practica2; 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; import static practica2.Lista.getString; import static practica2.Lista.webClient; /** * * @author Carlos */ public class Matriz extends javax.swing.JFrame { /** * Creates new form Matriz */ public Matriz() { initComponents(); } public static OkHttpClient webClient = new OkHttpClient(); @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); txtInsertar = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); txtEliminar = new javax.swing.JTextField(); btnEliminar = new javax.swing.JButton(); txtBuscarLetra = new javax.swing.JTextField(); btnBuscarLetra = new javax.swing.JButton(); txtBuscarDominio = new javax.swing.JTextField(); btnBuscarDominio = new javax.swing.JButton(); jLabel1.setText("Operaciones con matriz dispersa"); jButton1.setText("Insertar"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); btnEliminar.setText("Eliminar"); btnEliminar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEliminarActionPerformed(evt); } }); btnBuscarLetra.setText("Buscar Letra"); btnBuscarLetra.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBuscarLetraActionPerformed(evt); } }); btnBuscarDominio.setText("Buscar Dominio"); btnBuscarDominio.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBuscarDominioActionPerformed(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(91, 91, 91).addComponent(jButton1)) .addGroup(layout.createSequentialGroup().addGap(91, 91, 91).addComponent(btnEliminar)) .addGroup(layout.createSequentialGroup().addGap(53, 53, 53).addGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(txtInsertar).addComponent(txtEliminar)))) .addContainerGap(52, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10).addComponent(btnBuscarDominio)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(txtBuscarDominio, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(txtBuscarLetra, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup().addGap(9, 9, 9) .addComponent(btnBuscarLetra))))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addComponent(jLabel1).addGap(18, 18, 18) .addComponent(txtInsertar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jButton1).addGap(18, 18, 18) .addComponent(txtEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(btnEliminar).addGap(18, 18, 18) .addComponent(txtBuscarLetra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(btnBuscarLetra).addGap(18, 18, 18) .addComponent(txtBuscarDominio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnBuscarDominio).addContainerGap(92, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String correo = txtInsertar.getText(); RequestBody formBody = new FormEncodingBuilder().add("correo", correo).build(); String r = getString("insertarmatriz", formBody); System.out.println(r + "---"); }//GEN-LAST:event_jButton1ActionPerformed private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed String correo = txtEliminar.getText(); RequestBody formBody = new FormEncodingBuilder().add("correo", correo).build(); String r = getString("eliminarmatriz", formBody); System.out.println(r + "---"); }//GEN-LAST:event_btnEliminarActionPerformed private void btnBuscarLetraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarLetraActionPerformed String letra = txtBuscarLetra.getText(); RequestBody formBody = new FormEncodingBuilder().add("letra", letra).build(); String r = getString("buscarletra", formBody); System.out.println(r + "---"); }//GEN-LAST:event_btnBuscarLetraActionPerformed private void btnBuscarDominioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarDominioActionPerformed String dominio = txtBuscarDominio.getText(); RequestBody formBody = new FormEncodingBuilder().add("dominio", dominio).build(); String r = getString("buscardominio", formBody); System.out.println(r + "---"); }//GEN-LAST:event_btnBuscarDominioActionPerformed 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 = webClient.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(practica2.Lista.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { java.util.logging.Logger.getLogger(practica2.Lista.class.getName()).log(Level.SEVERE, null, ex); } return null; } 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(Matriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Matriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Matriz.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Matriz.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 Matriz().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnBuscarDominio; private javax.swing.JButton btnBuscarLetra; private javax.swing.JButton btnEliminar; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JTextField txtBuscarDominio; private javax.swing.JTextField txtBuscarLetra; private javax.swing.JTextField txtEliminar; private javax.swing.JTextField txtInsertar; // End of variables declaration//GEN-END:variables }