天天看點

對Volley架構的一些接口進行封裝:VolleyAir

簡介

volleyair是在著名的谷歌開源的網絡架構volley的基礎上進行的二次封裝,并吸取了volleyplus的一些封裝經驗,使之能更有效

的在複雜的資料處理邏輯層進行網絡請求,使邏輯層的代碼更加清爽簡潔。之是以選擇volley進行封裝,是因為volley是一款極為高效的網絡請求框

架,并且開發自谷歌的android團隊。在其基礎上封裝适配過後,将更為有利于我們的應用開發。

對Volley架構的一些接口進行封裝:VolleyAir

使用方法

1.根據自己的業務需求,,在networkmoudle類中自定義請求位址以及參數

public taskhandle arrangegetnewslist(string requesttag, string cty, string category, int page, int row, string title) { 

    httprequest request = new httprequest(api_url + "news/getnews"); 

    request.addparameter("cty", cty); 

    request.addparameter("category", category); 

    request.addparameter("page", integer.tostring(page)); 

    request.addparameter("row", integer.tostring(row)); 

    request.addparameter("title", title); 

    request.setrequesttag(requesttag); 

    return center.arrange(request, volleypoststring); 

2.根據自己的業務需求,在datamoudle類中自定義如何解析接收到的網絡資料

public iddata parsenewslist() throws httpprocessexception { 

    try { 

        jsonobject json = tryextra(jsonobject.class); 

        iddata data = new iddata(json.optint("count", -1), null); 

        jsonarray array = json.optjsonarray("data"); 

        arraylist<newslistitem> list = new arraylist<newslistitem>(array == null ? 0 : array.length()); 

        data.data = list; 

        if (null != array) { 

            newslistitem item; 

            for (int i = 0; i < array.length(); ++i) { 

                json = array.getjsonobject(i); 

                item = new newslistitem(); 

                item.id = json.optstring("id"); 

                item.title = json.optstring("title"); 

                item.create_time = json.optstring("create_time"); 

                item.img = json.optstring("img"); 

                item.category_name = json.optstring("category_name"); 

                item.city_name = json.optstring("city_name"); 

                item.description = json.optstring("description"); 

                list.add(item); 

            } 

        } 

        extra = data; 

        return data; 

    } catch (exception e) { 

        throw badresponseexception(e); 

    } 

3.讓view層(activity、fragment等)實作網絡資料接收器接口

public class mainactivity extends appcompatactivity implements receiver<datamodule> 

4.在view層(activity、fragment等)中進行請求,及結果處理

/** 

  * 可以并發多個網絡請求,通過每個請求task的id在view層的回調接口中處理請求結果 

  */ 

 private void beginvolley(){ 

     taskhandle handle_0 = networkmodule.arrangegetnewslist("arrangegetnewslist", null, null, 1, 10, null); 

     handle_0.setid(0); 

     handle_0.setreceiver(this); 

     handle_0.pulltrigger(); 

     taskhandle handle_1 = networkmodule.arrangeuploadimg("arrangeuploadimg", "path"); 

     handle_1.setid(1); 

     handle_1.setreceiver(this); 

     handle_1.pulltrigger(); 

 } 

 /** 

  * 網絡請求成功,處理結果 

  * @param handle 

  * @param result 

 @override 

 public void onsucess(taskhandle handle, datamodule result) { 

     switch (handle.id()){ 

         case 0: 

             if(result.code() == datamodule.codesucess){ 

             } 

             txt_1.settext(result.tostring()); 

             break; 

         case 1: 

             txt_2.settext(result.tostring()); 

     } 

  * 網絡請求異常,處理結果 

  * @param error 

 public void onerror(taskhandle handle, throwable error) { 

來源:51cto

繼續閱讀