Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android之Http通信——4.Android HTTP請求方式:HttpClient

Android之Http通信——4.Android HTTP請求方式:HttpClient

編輯:關於Android編程


本節引言:

上節講了HttpURLConnection,本節就到HttpClient了,Apache給我們提供的HttpClient(簡單的Http客戶端),
不過畢竟不是親兒子,HttpClient在API 21版本後就給Google棄用了,而我們實際開發中,很多頁面都不是通過
一個簡單的URL就可以訪問的,可能需要登陸或者相關權限才可以訪問,這就涉及到了Session,Cookie等的問題了;
當然我們可以用HttpURLConnection來實現,但是有點麻煩,而用HttpClient可以簡單點;HttpClient用於接收/發送
Http請求/響應,但不緩存服務器響應,不執行HTML頁面潛入的JS代碼,不會對頁面內容進行任何解析,處理;
要改掉廢話太多的習慣,SO簡化博文,開始本節內容吧:


HttpClient使用流程:


基本流程:

HttpClient使用流程


HttpClient使用示例:

1.發送GET請求

嗯,就寫個簡單的發送GET請求的代碼吧:

package com.example.httpclientdemo;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

    private Button btnGet;
    private WebView wView;
    public static final int SHOW_DATA = 0X123;
    private String detail = "";

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if(msg.what == SHOW_DATA)
            {
                wView.loadDataWithBaseURL("",detail, "text/html","UTF-8","");
            }
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setView();
    }

    private void initView() {
        btnGet = (Button) findViewById(R.id.btnGet);
        wView = (WebView) findViewById(R.id.wView);
    }

    private void setView() {
        btnGet.setOnClickListener(this);
        wView.getSettings().setDomStorageEnabled(true);
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnGet) {
            GetByHttpClient();
        }
    }
    private void GetByHttpClient() {
        new Thread()
        {
            public void run() 
            {
                    try {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet("http://www.w3cschool.cc/python/python-tutorial.html");
                        HttpResponse httpResponse = httpClient.execute(httpGet);
                        if (httpResponse.getStatusLine().getStatusCode() == 200) {
                            HttpEntity entity = httpResponse.getEntity();
                            detail = EntityUtils.toString(entity, "utf-8");
                            handler.sendEmptyMessage(SHOW_DATA);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            };
        }.start();
    }

}

另外,如果是帶有參數的GET請求的話,我們可以把參數放到List集合中,在對參數進行URL編碼:
然後和URL拼接

List params = new LinkedList();  
params.add(new BasicNameValuePair("user", "豬小弟"));  
params.add(new BasicNameValuePair("pawd", "123"));
String param = URLEncodedUtils.format(params, "UTF-8"); 
HttpGet httpGet = new HttpGet("http://www.baidu.com"+"?"+param);

接著貼下運行截圖:

運行截圖


2.發送POST請求

POST請求比GET稍微復雜一點,創建完HttpPost對象後,通過NameValuePair集合來存儲等待提交
的參數,並將參數傳遞到UrlEncodedFormEntity中,最後調用setEntity(entity)完成,
HttpClient.execute(HttpPost)即可;這裡就不寫例子了,暫時沒找到Post的網站,又不想
自己寫個Servlet,So,直接貼核心代碼吧~

核心代碼:

    private void PostByHttpClient(final String url)
    {
        new Thread()
        {
            public void run() 
            {
                try{
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    List params = new ArrayList();
                    params.add(new BasicNameValuePair("user", "豬大哥"));
                    params.add(new BasicNameValuePair("pawd", "123"));
                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,"UTF-8");
                    httpPost.setEntity(entity);
                    HttpResponse httpResponse =  httpClient.execute(httpPost);
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity2 = httpResponse.getEntity();
                        detail = EntityUtils.toString(entity2, "utf-8");
                        handler.sendEmptyMessage(SHOW_DATA);
                    }
                }catch(Exception e){e.printStackTrace();}
            };
        }.start();
    }

3.胡說八道

其實關於HttpClient的例子有很多,比如筆者曾經用它來抓學校教務系統上學生的課程表:
這就涉及到Cookie,模擬登陸的東西,說到抓數據(爬蟲),一般我們是搭配著JSoup來解析
抓到數據的,有興趣可以自己查閱相關資料,至於筆者的畢設,代碼很爛的說,後續有時間
整理在發布出來吧,這裡貼下模擬登陸教務系統部分的代碼,大家可以體會下HttpClient:

//獲得鏈接,模擬登錄的實現:
    public int getConnect(String user, String key) throws Exception {
        // 先發送get請求 獲取cookie值和__ViewState值
        HttpGet getLogin = new HttpGet(true_url);
        // 第一步:主要的HTML:
        String loginhtml = "";
        HttpResponse loginResponse = new DefaultHttpClient().execute(getLogin);
        if (loginResponse.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = loginResponse.getEntity();
            loginhtml = EntityUtils.toString(entity);
            // 獲取響應的cookie值
            cookie = loginResponse.getFirstHeader("Set-Cookie").getValue();
            System.out.println("cookie= " + cookie);
        }

        // 第二步:模擬登錄
        // 發送Post請求,禁止重定向
        HttpPost httpPost = new HttpPost(true_url);
        httpPost.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false);

        // 設置Post提交的頭信息的參數
        httpPost.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
        httpPost.setHeader("Referer", true_url);
        httpPost.setHeader("Cookie", cookie);

        // 設置請求數據
        List params = new ArrayList();

        params.add(new BasicNameValuePair("__VIEWSTATE",
                getViewState(loginhtml)));// __VIEWSTATE參數,如果變化可以動態抓取獲取
        params.add(new BasicNameValuePair("Button1", ""));
        params.add(new BasicNameValuePair("hidPdrs", ""));
        params.add(new BasicNameValuePair("hidsc", ""));
        params.add(new BasicNameValuePair("lbLanguage", ""));
        params.add(new BasicNameValuePair("RadioButtonList1", "%D1%A7%C9%FA"));
        params.add(new BasicNameValuePair("txtUserName", user));
        params.add(new BasicNameValuePair("TextBox2", key));
        params.add(new BasicNameValuePair("txtSecretCode", "")); // ( ╯□╰ )逗比正方,竟然不需要驗證碼

        // 設置編碼方式,響應請求,獲取響應狀態碼:
        httpPost.setEntity(new UrlEncodedFormEntity(params, "gb2312"));
        HttpResponse response = new DefaultHttpClient().execute(httpPost);
        int Status = response.getStatusLine().getStatusCode();
        if(Status == 200)return Status;
        System.out.println("Status= " + Status);

        // 重定向狀態碼為302
        if (Status == 302 || Status == 301) {
            // 獲取頭部信息中Location的值
            location = response.getFirstHeader("Location").getValue();
            System.out.println(location);
            // 第三步:獲取管理信息的主頁面
            // Get請求
            HttpGet httpGet = new HttpGet(ip_url + location);// 帶上location地址訪問
            httpGet.setHeader("Referer", true_url);
            httpGet.setHeader("Cookie", cookie);

            // 主頁的html
            mainhtml = "";
            HttpResponse httpResponseget = new DefaultHttpClient()
                    .execute(httpGet);
            if (httpResponseget.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = httpResponseget.getEntity();
                mainhtml = EntityUtils.toString(entity);
            }

        }
        return Status;
    }

總結:

好的,本節關於HttpClient就到這裡吧,內容還是比較簡單的~
下節我們將使用retrofit來封裝我們的HTTP請求,敬請期待~
對了,關於Android裡簡單的網絡技術可以參考小豬之前寫過的入門系列:
 


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved