We do in the project must not have the network request, now many companies network request this piece is a lot of use some relatively good open source framework, my project uses is volley, now talk about some volley basic use, if want detailed understanding will go to see its source, Now put the code to say some of its use,
First use a class to encapsulate its basic configuration information, later need to change, it is directly here to make changes, Android best can be independent of each function, so do not cause the modification will lead to other bugs, especially in the new employees into the project team, the allocation of tasks to communicate also easy,
Protocolmanager.java It is a single case
Key code
Private Protocolmanager (Context context) {Initloadbitmapconfig (context); Mqueue = Volley.newrequestqueue (context);}
We only need to do this when we tune the interface:
map<string,string> param = new hashmap<string, string> ();p aram.put ("checksum", 0+ ""); Protocolmanager.getinstance (). Request (Protocolenum.url_constance_conf_regions,param,new RequestListener () {@ overridepublic void Onrequestsucess (String response) {if (! Textutils.isempty (response)) {Allprovinceinfo Provinceinfo = Jsonhelper.json2bean (response, Allprovinceinfo.class); if (Provinceinfo!=null&&provinceinfo.getret () ==1) {provinces = Provinceinfo.getitems (); if (Provinces!=null&&provinces.size () >0) {/** * Cache the requested JSON data into the SD card */ Datamanager.getinstance (Uiutils.getcontext ()). SAVECACHESTR (Cache_area, response); Mhandler.sendemptymessage (area _code_success);} else {mhandler.sendemptymessage (area_code_empty);}}}} @Overridepublic void onrequestfiled (String errormsg) {mhandler.sendemptymessage (area_code_faial);}});
The method in Request
Public void Request (Protocolenum protocolenum,final map<string,string> params,final Requestlistener Listener) {int type = Protocolenum.type; String url = protocolenum.value;switch (type) {case ConfigManager.REQ_POST:requestPost (URL, Params,listener, false); Break;case ConfigManager.REQ_GET:requestGet (URL, Params,listener, false); break;}}
The first parameter is an enumeration that contains the URL address and the request method
Protocolenum.java
public enum Protocolenum {url_profile_introduction_get ("user/profile/introduction", configmanager.req_get);// Drop-down profile public string value, public int type;private protocolenum (String value, int type) {this.value = Value;this.type = Type;}}
This is how to manage the URL of the request, finally in the volley request it will have a successful or failed callback, the JSON data, so we add an interface later, as long as copy, change the returned model object, so the development efficiency greatly improved
The network request framework used in the Android project and how to configure the interface URL