Android下網路請求的兩個封裝類

來源:互聯網
上載者:User

Android下網路請求的兩個封裝類
1、背景介紹

我們在Activity和Fragment中做網路請求的時候,往往用到線程和handler訊息處理機制等做處理。這裡,我們將網路請求,一個get和一個post請求,使用線程封裝一下。這樣就剝離開了Activity中的代碼量,將複雜的代碼,封裝在不同的類中,實現一個類的單一原則。


2、GET請求封裝

/** * GET提交的網路請求
*
* 傳遞過來主UI線程的訊息處理器,經過線程處理之後,返回一個訊息給主UI線程
* 在返回的msg資訊中,what封裝了返回的狀態代碼資訊
* 1001代表成功,404代表用戶端請求錯誤,100代表IO處理錯誤
*
* obj資訊封裝了請求的結果,傳回值為String類型 * * @author ljtyzhr * */public class HttpGetThread implements Runnable {/** handler處理 */private Handler handler;/** 網路請求地址 */private String url;/** * 使用Get提交的構造方法 * * @param handler * 訊息對象 * @param url * 請求的url地址 */public HttpGetThread(Handler handler, String url) {this.handler = handler;this.url = url;}@Overridepublic void run() {// 擷取我們回調主ui的messageMessage msg = handler.obtainMessage();try {String result = HttpRequestUtils.doGet(url);msg.what = ConstantValues.GET_NET_SUCCEED;msg.obj = result;} catch (ClientProtocolException e) {msg.what = 404;} catch (IOException e) {msg.what = 100;}// 給主ui發送訊息傳遞資料handler.sendMessage(msg);}}

3、POST請求封裝

/** * POST提交的網路請求
*
* 傳遞過來主UI線程的訊息處理器,經過線程處理之後,返回一個訊息給主UI線程
* 在返回的msg資訊中,what封裝了返回的狀態代碼資訊
* 1001代表成功,404代表用戶端請求錯誤,100代表IO處理錯誤
*
* obj資訊封裝了請求的結果,傳回值為String類型 * * @author ljtyzhr * */public class HttpPostThread implements Runnable {/** handler處理 */private Handler handler;/** 網路請求地址 */private String url;/** * 使用post提交的構造方法 * * @param handler * 訊息對象 * @param url * 請求的url地址 */public HttpPostThread(Handler handler, String url) {this.handler = handler;this.url = url;}@Overridepublic void run() {// 擷取我們回調主ui的messageMessage msg = handler.obtainMessage();try {String result = HttpRequestUtils.doPost(url);msg.what = ConstantValues.GET_NET_SUCCEED;msg.obj = result;} catch (ClientProtocolException e) {msg.what = 404;} catch (IOException e) {msg.what = 100;}// 給主ui發送訊息傳遞資料handler.sendMessage(msg);}}
4、使用方式

看到上面的參數資訊,我們大致知道需要一些怎樣的參數,這裡簡單介紹如何使用這兩個封裝類:

給出一個Get請求的執行個體:

HttpGetThread httpGetThread = new HttpGetThread(handler, url, this);new Thread(httpGetThread).start();

handler的處理機制:

private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case ConstantValues.GET_NET_SUCCEED:// 擷取網路成功jsonString = (String) msg.obj;break;case ConstantValues.GET_NET_FAILED:// 擷取網路失敗break;case ConstantValues.DEL_RESULT_SUCCEED:// 資料處理成功了,開始設定資料break;case ConstantValues.DEL_RESULT_FAILED:// 字串處理失敗theUrlErrorShow();break;case ConstantValues.THE_URL_ERROR:// 字串處理失敗Logger.ii(TAG, "The url error!");break;default:break;}}};

5、總結

在使用上面的網路請求封裝類的時候,要注意兩個問題:

第一、我們使用的網路請求中,需要傳遞當前的handler處理對象,用於接收我們的訊息處理返回資訊;內部的網路請求,get和post請求,這裡不再贅述;

第二、我們使用的這個架構中,沒有使用到Cookie處理機制,那麼當我們做需要登入的請求的時候,需要使用Cookie處理。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.