适当的HTTP客户端方法

时间:2012-04-23 17:23:34

标签: java android 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";
        }

        }

1 个答案:

答案 0 :(得分:1)

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

无论如何,这里是我的代码,它完全复制你的代码(在我的情况下只有Editable是String)并且应该是可编译的并且可以按原样运行。对我来说它有效。每次我点击按钮都会执行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;

}