編輯:關於Android編程
一個好用的網絡底層框架可以很大的程度上方便自己的項目,我們下面要做的就是一個趁手的網絡框架。
做一個網絡框架我們首先要確定這個網絡框架除了能夠從網絡上獲取數據還需要哪些功能:
首先拋棄AsyncTask,自定義一套網絡底層的封裝框架。設計一套適合自己App的緩存策略設計一套假數據返回的機制,在網絡請求API沒有返回的時候,可以假裝獲取到了網絡返回的數據。封裝用戶cookie的邏輯。其他的還好,可能有人對於第一步的那個拋棄AsyncTask有些疑問,就是為什麼要拋棄啊,這個類這個好用,內部封裝了那麼多的方法。但是我們不能只看到這個的優點,這個類有個致命的缺點:不能靈活的控制內部的線程池。
我們都知道的是,線程池裡面的每個線程都是API的調用請求,而AsyncTask中有沒有暴漏出取消這些請求的方法,這個時候,如果我們從A界面調到B界面,那麼在A界面調用的API請求,如果還沒有返回,並不會被取消,對於一個頻繁調用API請求的APP應用應用來說,一個界面調用的API可能超過十個,在網絡不好的情況下,如果這個時候跳轉到了其他界面,這個時候其他界面也會調用API,這個時候造成的情況就是這個界面的請求並不會顯示數據,因為首頁的請求還在排隊,要等首頁的請求完成之後你才可以調用,這個就是所謂的AsyncTask堵塞。
我剛工作的時候遇到一個情況就是,根據公司的情況寫了一個統計用戶交互數據的SDK,開始的使用時候就是這個AsyncTask類,結構我發現在APP中某一個界面的吊起特別的慢,數據加載也非常的慢,發現的原因是我寫的這個SDK中的API請求調用超時,並且在超時的時候重復調用三次這個API。
網絡請求兩個方法POST和GET,我們一般把GET方法為請求數據,POST為修改數據。請求的方法格式也是相對有講究的。
所有的MobileApi都可以寫作:http://www.xxx.com/aaaa.api的形式。
GET:對於GET方法我們可以將請求API寫作http://www.xxx.com/aaaa.api K1=va1&K2=va2,形式,也就是說,把key-value這樣的鍵值對存放在URL上,這樣做的話會方便我們後面對數據進行緩存,另外要精良是GET的參數都是String,int這樣的類型,方便緩存,解析。POST:我們都知道看不見POST的請求數據,一般key-value這樣的鍵值對存放在Form表單中,最後進行提交請求。POST經常會提交大量數據,所以有些鍵值對要定義成集合或復雜的自定義實例,這個時候我們就需要把這樣的值轉換為JSON字符串進行提交,有APP傳遞到API後,在將JSON字符串轉換為對於的實體。服務器現在用的最多的是使用JSON作為api返回的結果,這裡也是使用JSON。
一般情況下返回的json數據中要有以下數據:
首先一個是否調用api成功的參數,另外一個錯誤類型的參數(這個參數可以是Int格式的參數,成功為0)錯誤具體信息的參數,成功為“”具體API返回的結果,失敗為“”所以我們定義一個Response實體類,作為JSON實體的最外層。
package com.infrastructure.net; /** * Created by binbin.ma on 2016/6/25. */ public class Response { private boolean error ; private int errorType ; private String errorMessage ; private String result ; public boolean hasError() { return error; } public void setError(boolean hasError) { this.error = hasError; } public int getErrorType() { return errorType; } public void setErrorType(int errorType) { this.errorType = errorType; } public String getErrorMessage() { return errorMessage; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } }
如果上面返回的result是一種實體的集合,那麼就要把result解析為相應的實體集合。
我們在前面看到我們把AsyncTask拋棄重新寫一個擴展性強的,可以隨時取消API請求網絡底層,那麼我們的這個網絡底層的線程池使用的是什麼:使用原生的ThreadPoolExecutor + Runnable + Handler
首先我們要把App所調用的所有的API接口放到一個類或者xml文件中去,我們這裡放在xml文件裡面去,當然要寫出讀取xml的類和函數:
package com.infrastructure.net; import android.app.Activity; import android.content.res.XmlResourceParser; import com.infrastructure.R; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** */ public class UrlConfigManager { private static ArrayListurlList ; private static void fetchUrlDataFromXml(final Activity activity) { urlList = new ArrayList (); final XmlResourceParser xmlParser = activity.getApplication() .getResources().getXml(R.xml.url); int eventCode; try { eventCode = xmlParser.getEventType(); while (eventCode != XmlPullParser.END_DOCUMENT) { switch (eventCode) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if ("Node".equals(xmlParser.getName())) { final String key = xmlParser.getAttributeValue(null, "Key"); final URLData urlData = new URLData(); urlData.setKey(key); urlData.setExpires(Long.parseLong(xmlParser .getAttributeValue(null, "Expires"))); urlData.setNetType(xmlParser.getAttributeValue(null, "NetType")); urlData.setMockClass(xmlParser.getAttributeValue(null, "MockClass")); urlData.setUrl(xmlParser.getAttributeValue(null, "Url")); urlList.add(urlData); } break; case XmlPullParser.END_TAG: break; default: break; } eventCode = xmlParser.next(); } } catch (final XmlPullParserException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } finally { xmlParser.close(); } } public static URLData findURL(final Activity activity, final String findKey) { // 如果urlList還沒有數據(第一次),或者被回收了,那麼(重新)加載xml if (urlList == null || urlList.isEmpty()) fetchUrlDataFromXml(activity); for (URLData data : urlList) { if (findKey.equals(data.getKey())) { return data; } } return null; } }
package com.infrastructure.net; /** */ public class URLData { private String key ; private long expires ; private String netType ; private String url ; private String mockClass ; public URLData( ) { } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public long getExpires() { return expires; } public void setExpires(long expires) { this.expires = expires; } public String getNetType() { return netType; } public void setNetType(String netType) { this.netType = netType; } public String getMockClass() { return mockClass; } public void setMockClass(String mockClass) { this.mockClass = mockClass; } }
其中key和url的值符合key-value鍵值,expires代表數據緩存的時間單位為毫秒,netType代表請求方式(POST和GET) ,mockClass代表的是返回假數據的類。
RemoteService和RequestCallback和RequestParameter
這三個類表示的是請求的服務,請求返回,請求參數,三個給APP調用的類。
package com.infrastructure.net; /** */ public interface RequestCallback { public void onSuccess(String content) ; public void onFail(String errorMessage) ; public void onCookieExpired(); }
package com.infrastructure.net; import java.io.Serializable; import java.util.Comparator; import java.util.Objects; /** */ public class RequestParameter implements Serializable,Comparator
package com.youngheart.engine; import android.util.Log; import java.util.List; import com.alibaba.fastjson.JSON; import com.infrastructure.activity.BaseActivity; import com.infrastructure.net.DefaultThreadPool; import com.infrastructure.net.HttpRequest; import com.infrastructure.net.RequestCallback; import com.infrastructure.net.RequestManager; import com.infrastructure.net.RequestParameter; import com.infrastructure.net.Response; import com.infrastructure.net.URLData; import com.infrastructure.net.UrlConfigManager; import com.youngheart.mockdata.MockService; public class RemoteService { private static RemoteService service = null; private static final String TAG = "RemoteService" ; private RemoteService() { } public static synchronized RemoteService getInstance() { if (RemoteService.service == null) { RemoteService.service = new RemoteService(); } return RemoteService.service; } public void invoke(final BaseActivity activity, final String apiKey, final Listparams, final RequestCallback callBack,final boolean forceUpdate) { final URLData urlData = UrlConfigManager.findURL(activity, apiKey); if (forceUpdate){ urlData.setExpires(0); } if (null == urlData.getMockClass()) { HttpRequest request = activity.getRequestManager().createRequest( urlData, params, callBack); DefaultThreadPool.getInstance().execute(request); }else { try { MockService mockService = (MockService) Class.forName(urlData.getMockClass()).newInstance(); String strResponse = mockService.getJSONData(); final Response responseInJSON = JSON.parseObject(strResponse,Response.class) ; Log.d(TAG, "invoke: "+responseInJSON); if (callBack != null){ if (responseInJSON.hasError()){ callBack.onFail(responseInJSON.getErrorMessage()); }else { callBack.onSuccess(responseInJSON.getResult()); } } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } }
RemoteService.getInstance().invoke(Context context,String key,RequestParameters requestParameters,RequestCallback callback,boolean forceUpdate);其他的兩個類在方法中調用:
context:表示上下文key:即xml文件中的keyRequestParameter:請求攜帶的參數callback:請求回調forceUpdate:是否強制更新數據,忽略緩存
RequestMannager類是一個集合類,用於取消請求的。每次發起請求時,都會把為此創建的Request添加到RequestManager中,即RequestManager中保存了全部的request。
package com.infrastructure.net; import java.util.ArrayList; import java.util.List; import com.infrastructure.activity.BaseActivity; public class RequestManager { ArrayListrequestList = null; public RequestManager(final BaseActivity activity) { // 異步請求列表 requestList = new ArrayList (); } /** * 添加Request到列表 */ public void addRequest(final HttpRequest request) { requestList.add(request); } /** * 取消網絡請求 */ public void cancelRequest() { if ((requestList != null) && (requestList.size() > 0)) { for (final HttpRequest request : requestList) { if (request.getRequest() != null) { try { request.getRequest().abort(); requestList.remove(request.getRequest()); } catch (final UnsupportedOperationException e) { e.printStackTrace(); } } } } } /** * 無參數調用 */ public HttpRequest createRequest(final URLData urlData, final RequestCallback requestCallback) { return createRequest(urlData, null, requestCallback); } /** * 有參數調用 */ public HttpRequest createRequest(final URLData urlData, final List params, final RequestCallback requestCallback) { final HttpRequest request = new HttpRequest(urlData, params, requestCallback); addRequest(request); return request; } }
if (requestManager != null){ requestManager.cancelRequest(); }而這個網絡請求底層框架的線程池使用的是ThreadPoolExecutor創建的類是DefaultThreadPool:
package com.infrastructure.net; import java.util.concurrent.AbstractExecutorService; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 線程池 、緩沖隊列 * */ public class DefaultThreadPool { // 阻塞隊列最大任務數量 static final int BLOCKING_QUEUE_SIZE = 20; static final int THREAD_POOL_MAX_SIZE = 10; static final int THREAD_POOL_SIZE = 6; /** * 緩沖BaseRequest任務隊列 */ static ArrayBlockingQueueblockingQueue = new ArrayBlockingQueue ( DefaultThreadPool.BLOCKING_QUEUE_SIZE); private static DefaultThreadPool instance = null; /** * 線程池,目前是十個線程, */ static AbstractExecutorService pool = new ThreadPoolExecutor( DefaultThreadPool.THREAD_POOL_SIZE, DefaultThreadPool.THREAD_POOL_MAX_SIZE, 15L, TimeUnit.SECONDS, DefaultThreadPool.blockingQueue, new ThreadPoolExecutor.DiscardOldestPolicy()); public static synchronized DefaultThreadPool getInstance() { if (DefaultThreadPool.instance == null) { DefaultThreadPool.instance = new DefaultThreadPool(); } return DefaultThreadPool.instance; } public static void removeAllTask() { DefaultThreadPool.blockingQueue.clear(); } public static void removeTaskFromQueue(final Object obj) { DefaultThreadPool.blockingQueue.remove(obj); } /** * 關閉,並等待任務執行完成,不接受新任務 */ public static void shutdown() { if (DefaultThreadPool.pool != null) { DefaultThreadPool.pool.shutdown(); } } /** * 關閉,立即關閉,並掛起所有正在執行的線程,不接受新任務 */ public static void shutdownRightnow() { if (DefaultThreadPool.pool != null) { DefaultThreadPool.pool.shutdownNow(); try { // 設置超時極短,強制關閉所有任務 DefaultThreadPool.pool.awaitTermination(1, TimeUnit.MICROSECONDS); } catch (final InterruptedException e) { e.printStackTrace(); } } } /** * 執行任務 * * @param r */ public void execute(final Runnable r) { if (r != null) { try { DefaultThreadPool.pool.execute(r); } catch (final Exception e) { e.printStackTrace(); } } } }
HttpRequest類,發起HTTP請求的地方,他事先了Runable,從而讓DefaultThreadPool可以分配新的線程,所以所有的請求邏輯都在Runnable接口方法裡:
package com.infrastructure.net; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.TimeZone; import java.util.zip.GZIPInputStream; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.os.Handler; import android.preference.PreferenceActivity; import android.util.Log; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.infrastructure.cache.CacheManager; import com.infrastructure.utils.BaseUtils; import com.infrastructure.utils.FrameConstants; public class HttpRequest implements Runnable { private static final String TAG = "HttpRequest" ; // 區分get還是post的枚舉 public static final String REQUEST_GET = "get"; public static final String REQUEST_POST = "post"; private final static String cookiePath = "/data/data/com.youngheart/cache/cookie"; private HttpUriRequest request = null; private URLData urlData = null; private RequestCallback requestCallback = null; private Listparameter = null; private String url = null; // 原始url private String newUrl = null; // 拼接key-value後的url private HttpResponse response = null; private DefaultHttpClient httpClient; // 切換回UI線程 protected Handler handler; protected boolean cacheRequestData = true; HashMap headers ; static long deltaBetweenServerAndClientTime; // 服務器時間和客戶端時間的差值 public HttpRequest(final URLData data, final List params, final RequestCallback callBack) { urlData = data; url = urlData.getUrl(); this.parameter = params; requestCallback = callBack; if (httpClient == null) { httpClient = new DefaultHttpClient(); } handler = new Handler(); headers = new HashMap<>() ; } /** * 獲取HttpUriRequest請求 * * @return */ public HttpUriRequest getRequest() { return request; } @Override public void run() { try { if (urlData.getNetType().equals(REQUEST_GET)) { // 添加參數 final StringBuffer paramBuffer = new StringBuffer(); if ((parameter != null) && (parameter.size() > 0)) { // 這裡要對key進行排序 sortKeys(); for (final RequestParameter p : parameter) { if (paramBuffer.length() == 0) { paramBuffer.append(p.getName() + "=" + BaseUtils.UrlEncodeUnicode(p.getValue())); } else { paramBuffer.append("&" + p.getName() + "=" + BaseUtils.UrlEncodeUnicode(p.getValue())); } } newUrl = url + "?" + paramBuffer.toString(); } else { newUrl = url; } // 如果這個get的API有緩存時間(大於0) if (urlData.getExpires() > 0) { final String content = CacheManager.getInstance() .getFileCache(newUrl); if (content != null) { Log.d(TAG,"handle.post") ; handler.post(new Runnable() { @Override public void run() { requestCallback.onSuccess(content); } }); return; } } request = new HttpGet(newUrl); } else if (urlData.getNetType().equals(REQUEST_POST)) { request = new HttpPost(url); // 添加參數 if ((parameter != null) && (parameter.size() > 0)) { final List list = new ArrayList (); for (final RequestParameter p : parameter) { list.add(new BasicNameValuePair(p.getName(), p .getValue())); } ((HttpPost) request).setEntity(new UrlEncodedFormEntity( list, HTTP.UTF_8)); } } else { return; } request.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 30000); request.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000); /** * 添加必要的頭信息 */ setHttpHeaders(request) ; // 添加Cookie到請求頭中 addCookie(); // 發送請求 response = httpClient.execute(request); // 獲取狀態 final int statusCode = response.getStatusLine().getStatusCode(); // 設置回調函數,但如果requestCallback,說明不需要回調,不需要知道返回結果 if ((requestCallback != null)) { if (statusCode == HttpStatus.SC_OK) { updateDeltaBetweenServerAndClientTime(); final ByteArrayOutputStream content = new ByteArrayOutputStream(); String strResponse = "" ; if ((response.getEntity().getContentEncoding() != null)&&(response.getEntity().getContentEncoding().getValue() != null)){ if (response.getEntity().getContentEncoding().getValue().contains("gzip")){ final InputStream in = response.getEntity().getContent(); final InputStream is = new GZIPInputStream(in) ; strResponse = HttpRequest.inputStreamToString(is) ; is.close(); }else{ response.getEntity().writeTo(content); strResponse = new String(content.toByteArray()).trim(); } }else{ response.getEntity().writeTo(content); strResponse = new String(content.toByteArray()).trim(); } Log.d(TAG, "run: ----------------------------->"+strResponse); final Response responseInJson = new Response(); if (strResponse == null) { responseInJson.setError(true); responseInJson.setErrorMessage("網絡異常"); } else { responseInJson.setError(false); responseInJson.setResult(strResponse); } if (responseInJson.hasError()) { if (responseInJson.getErrorType() == 1){ handler.post(new Runnable() { @Override public void run() { requestCallback.onCookieExpired(); } }) ; }else{ handleNetworkError(responseInJson.getErrorMessage()); } } else { // 把成功獲取到的數據記錄到緩存 if (urlData.getNetType().equals(REQUEST_GET) && urlData.getExpires() > 0) { CacheManager.getInstance().putFileCache(newUrl, responseInJson.getResult(), urlData.getExpires()); } Log.d(TAG, "run: handler.post"); handler.post(new Runnable() { @Override public void run() { requestCallback.onSuccess(responseInJson .getResult()); } }); saveCookie(); } } else { handleNetworkError("網絡異常"); } } else { handleNetworkError("網絡異常"); } } catch (final java.lang.IllegalArgumentException e) { handleNetworkError("網絡異常"); } catch (final UnsupportedEncodingException e) { handleNetworkError("網絡異常"); } catch (final IOException e) { handleNetworkError("網絡異常"); } } public void handleNetworkError(final String errorMsg) { if (requestCallback != null) { handler.post(new Runnable() { @Override public void run() { requestCallback.onFail(errorMsg); } }); } } /** * cookie列表保存到本地 * * @return */ public synchronized void saveCookie() { // 獲取本次訪問的cookie final List cookies = httpClient.getCookieStore().getCookies(); // 將普通cookie轉換為可序列化的cookie List serializableCookies = null; if ((cookies != null) && (cookies.size() > 0)) { serializableCookies = new ArrayList (); for (final Cookie c : cookies) { serializableCookies.add(new SerializableCookie(c)); } } BaseUtils.saveObject(cookiePath, serializableCookies); } void setHttpHeaders(final HttpUriRequest httpMessage){ headers.clear(); headers.put(FrameConstants.ACCEPT_CHARSET,"UTF-8") ; headers.put(FrameConstants.USER_AGENT,"Young Heart Android App ") ; headers.put(FrameConstants.ACCEPT_ENCODING,"gzip") ; if ((httpMessage != null)&&(headers != null)){ for (final Map.Entry entry:headers.entrySet()){ if (entry.getKey() != null){ httpMessage.addHeader(entry.getKey(),entry.getValue()); } } } } static String inputStreamToString(final InputStream is) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = -1; while ((i = is.read()) != -1) { baos.write(i); } return baos.toString(); } void sortKeys() { for (int i = 1; i < parameter.size(); i++) { for (int j = i; j > 0; j--) { RequestParameter p1 = parameter.get(j - 1); RequestParameter p2 = parameter.get(j); if (compare(p1.getName(), p2.getName())) { // 交互p1和p2這兩個對象,寫的超級惡心 String name = p1.getName(); String value = p1.getValue(); p1.setName(p2.getName()); p1.setValue(p2.getValue()); p2.setName(name); p2.setValue(value); } } } } // 返回true說明str1大,返回false說明str2大 boolean compare(String str1, String str2) { String uppStr1 = str1.toUpperCase(); String uppStr2 = str2.toUpperCase(); boolean str1IsLonger = true; int minLen = 0; if (str1.length() < str2.length()) { minLen = str1.length(); str1IsLonger = false; } else { minLen = str2.length(); str1IsLonger = true; } for (int index = 0; index < minLen; index++) { char ch1 = uppStr1.charAt(index); char ch2 = uppStr2.charAt(index); if (ch1 != ch2) { if (ch1 > ch2) { return true; // str1大 } else { return false; // str2大 } } } return str1IsLonger; } @SuppressWarnings("unchecked") public void addCookie(){ List cookieList = null ; Object cookieObject = BaseUtils.restoreObject(cookiePath); if (cookieObject != null){ cookieList = (List ) cookieObject; } if ((cookieList!=null)&&(cookieList.size()>0)){ final BasicCookieStore cs = new BasicCookieStore(); cs.addCookies(cookieList.toArray(new Cookie[] {})); httpClient.setCookieStore(cs); }else { httpClient.setCookieStore(null); } } /** * 更新服務器時間和本地時間的差值 */ void updateDeltaBetweenServerAndClientTime(){ if (response != null){ final Header header = response.getLastHeader("Date") ; if (header != null){ final String strServerDate = header.getValue(); try{ if ((strServerDate != null)&& strServerDate.equals("")){ final SimpleDateFormat sdf = new SimpleDateFormat("EEE,d MMM yyyy HH:mm:ss z", Locale.ENGLISH) ; TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); Date serverDateUAT = sdf.parse(strServerDate) ; deltaBetweenServerAndClientTime = serverDateUAT.getTime() + 8 * 60 * 60 * 1000 - System.currentTimeMillis(); } } catch (ParseException e) { e.printStackTrace(); } } } } public static Date getServerTime(){ return new Date(System.currentTimeMillis()+deltaBetweenServerAndClientTime) ; } }
需要注意的是,因為我們把每個HttpRequest都放在了子線程中執行,所以RequestCallback的回調不能直接操作UI線程的控件,所以這個時候Handler就可以用到了。使用這個就可以保證RequestCallback的回調在UI線程上,不會報錯。
首先,定義TabHost的布局文件:復制代碼 代碼如下:<?xml version=1.0 encoding=utf-8?><TabHost xmlns
在開發應用程序的時候,經常會遇到這樣的情況,會在運行時動態根據條件來決定顯示哪個View或某個布局。那麼最通常的想法就是把可能用到的View都寫在上面,先把它們的可見性都
可能部分小伙伴對小米發布的紅米3s這款升級機型還不怎麼清楚,而對比華為的剛剛發布的另一款新機華為榮耀5a,它們在價格上相差不遠,紅米3S和華為榮耀5A哪個好
本文實例講述的是AlertDialog,這種對話框會經常遇到。AlertDialog跟WIN32開發中的Dialog不一樣,AlertDialog是非阻塞的,而阻塞的對話