1

我正在尝试运行我创建的方法,但它不会运行多次。我认为我的问题是我永远不会释放连接。

我尝试按照本教程进行操作,但有些方法不起作用,我不知道为什么。 http://hc.apache.org/httpclient-3.x/tutorial.html

public String sendMessage(String username, Editable message){

        BufferedReader in = null;
        String data = null;
        try{


            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://abc.com/user_send.php?username="+username+"&message="+message);

            HttpPost post_request = new HttpPost();
            post_request.setURI(website);


            HttpGet request = new HttpGet();

            request.setURI(website);
            //executing actual request
                HttpResponse response = client.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) != null) {
                sb.append(l);

            }
            in.close();
            data = sb.toString();
            return data;
        }catch (Exception e){
            return "ERROR";
        }

        }
4

1 回答 1

1

首先,你知道post_request永远不习惯吗?只有 GET 方法 ( request) 被发送到服务器。

无论如何,这是我的代码,它完全复制了您的代码(在我的情况下,只有 Editable 是 String)并且应该是可编译和可运行的。对我来说它有效。每次我单击按钮时,request都会执行 GET。

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;


public class NewJFrame extends javax.swing.JFrame {

  /** Creates new form NewJFrame */
  public NewJFrame() {
    initComponents();
  }


  public String sendMessage(String username, String message){

    BufferedReader in = null;
    String data = null;
    try{


        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://abc.com/user_send.php?username="+username+"&message="+message);

        //!!!this is never used
        HttpPost post_request = new HttpPost();
        post_request.setURI(website);


        HttpGet request = new HttpGet();

        request.setURI(website);
        //executing actual request
            HttpResponse response = client.execute(request);

        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) != null) {
            sb.append(l);

        }
        in.close();

        data = sb.toString();
        System.out.println("Success:\n" + data);


        return data;
    }catch (Exception e){
        return "ERROR";
    }

  }


  @SuppressWarnings("unchecked")
  private void initComponents() {

    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(64, 64, 64)
            .add(jButton1)
            .addContainerGap(239, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(layout.createSequentialGroup()
            .add(58, 58, 58)
            .add(jButton1)
            .addContainerGap(213, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    sendMessage("ondra", "textX");
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
  }

  private javax.swing.JButton jButton1;

}
于 2012-04-24T06:03:27.807 回答