且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用httpclient的Android项目-->http.client (apache), post/get 方法

更新时间:2022-11-16 22:33:34

回答我的问题最简单的方法是向你展示我制作的课程:

The easiest way to answer my question is to show you the class that I made :

public class HTTPHelp{

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    private boolean abort;
    private String ret;

    HttpResponse response = null;
    HttpPost httpPost = null;

    public HTTPHelp(){

    }

    public void clearCookies() {

        httpClient.getCookieStore().clear();

    }

    public void abort() {

        try {
            if(httpClient!=null){
                System.out.println("Abort.");
                httpPost.abort();
                abort = true;
            }
        } catch (Exception e) {
            System.out.println("HTTPHelp : Abort Exception : "+e);
        }
    }

    public String postPage(String url, String data, boolean returnAddr) {

        ret = null;

        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

        httpPost = new HttpPost(url);
        response = null;

        StringEntity tmp = null;        

        httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
            "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
        httpPost.setHeader("Accept", "text/html,application/xml," +
            "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        try {
            tmp = new StringEntity(data,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("HTTPHelp : UnsupportedEncodingException : "+e);
        }

        httpPost.setEntity(tmp);

        try {
            response = httpClient.execute(httpPost,localContext);
        } catch (ClientProtocolException e) {
            System.out.println("HTTPHelp : ClientProtocolException : "+e);
        } catch (IOException e) {
            System.out.println("HTTPHelp : IOException : "+e);
        } 
                ret = response.getStatusLine().toString();

                return ret;
                }
}

我使用本教程来做我的发布方法和那些例子

I used this tutorial to do my post method and thoses examples