2012-04-23 129 views
1

我试图运行一个我创建的方法,但它不会运行多次。我认为我的问题是我永远不会释放连接。正确的HTTP客户端方法

我试图按照这个教程,但一些方法不起作用,我不知道为什么。 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"; 
     } 

     } 
+0

如何第一次和第二次调用sendMessage()方法? – yorkw 2012-04-23 22:59:41

+0

该方法被称为onClick我的布局上的“发送”按钮。 – EGHDK 2012-04-24 01:54:33

+0

究竟发生了什么?第二次打印错误?通过“方法”你的意思是sendMessage(字符串用户名,可编辑的消息)或http post方法? – 2012-04-24 05:50:08

回答

1

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

无论如何,这里是我的代码,完全复制你的(只有可编辑的字符串在我的情况),应该是可编译和可运行的。对我来说它有效。每次我点击按钮GET request执行。

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; 

} 
+0

感谢您的帮助和代码。欣赏它。 – EGHDK 2012-04-24 06:11:13