当前位置: 代码迷 >> Android >> android涓?殑璁捐?妯″紡-绛栫暐妯″紡
  详细解决方案

android涓?殑璁捐?妯″紡-绛栫暐妯″紡

热度:45   发布时间:2016-04-28 04:14:35.0
android涓?殑璁捐?妯″紡--绛栫暐妯″紡

鈥滅瓥鐣ユā寮忓畾涔変簡绠楁硶鏃忥紝鍒嗗埆灏佽?璧锋潵锛岃?浠栦滑涔嬮棿鍙?互浜掔浉鏇挎崲锛屾?妯″紡璁╃畻娉曠殑鍙樺寲鐙?珛浜庣畻娉曠殑瀹㈡埛鈥?/span>

鍦烘櫙锛?/span>
涓?涓?熀绫绘湁寰堝?涓?瓙绫伙紝鐜板湪浠栦滑闇?瑕佸疄鐜颁竴涓?叡鍚岀殑鍑芥暟锛屽嚒鏄?悇涓?瓙绫荤殑瀹炵幇鍙?兘鍚勬湁涓嶅悓锛岃繖鏃跺?欏簲璇ユ?庝箞鍔烇紵

鎬濊矾1锛氭妸杩欎釜鍑芥暟鍐欏湪鍩虹被閲岋紝鐒跺悗瀛愮被鍘绘牴鎹?嚜宸辩殑闇?瑕侀噸鍐欒繖涓?嚱鏁?/span>
濂藉?锛氫唬鐮佸彲浠ュ?鐢?紝瀵瑰簲浜嗚?璁″師鍒欌?滄壘鍑哄簲鐢ㄤ腑鍙?兘鍙樺寲涔嬪?锛屾妸瀹冪嫭绔嬪嚭鏉ワ紝涓嶈?鍜岄偅浜涗笉鍙樼殑浠g爜娣峰湪涓?璧封??/span>
鍧忓?锛氬彲鑳戒細鏈夊瓙绫讳笉灏忓績蹇樹簡鍐欎簡锛屽氨鐩存帴缁ф壙浜嗗熀绫荤殑瀹炵幇锛岃繖鏍风殑閿欒?姣旇緝闅炬煡

鎬濊矾2锛氱洿鎺ユ妸杩欎釜鏂规硶鍋氭垚鎺ュ彛鏀惧埌鍩虹被閲岋紝鐒跺悗瀛愮被鍘诲疄鐜?/span>
濂藉?锛氫唬鐮佺殑鐏垫椿鎬у緢濂斤紝鍚勪釜瀛愮被淇?敼鑷?繁鐨勬柟娉曚笉浼氬奖鍝嶅埆浜猴紝瀵瑰簲浜嗚?璁″師鍒欌?滃?鐢ㄧ粍鍚?灏戠敤缁ф壙鈥?/span>
鍧忓?锛氫唬鐮佹棤娉曞?鐢?/span>

鎵?浠ワ紝杩欎袱鑰呮壃闀块伩鐭?紝褰㈡垚浜嗕竴涓?繖鏍风殑瑙e喅鎬濊矾锛?/span>
1. 鍏堜竴涓?柊鐨勬帴鍙o紝鐢ㄦ潵瑙e喅闂??锛岀劧鍚庡仛鍑哄畠鐨勫疄鐜颁竴绯诲垪銆?/span>
2. 鍦ㄧ埗绫婚噷瀹炰緥鍖栦竴涓?繖涓?帴鍙c?傜劧鍚庡湪閭d釜鍑芥暟閲岃皟鐢ㄨ繖涓?帴鍙g殑鏂规硶瑙e喅闂??
3.瀛愮被涓鸿繖涓?帴鍙i?夋嫨瀹炵幇

杩欏氨绛変簬鎶婅繖涓?嚱鏁扮殑瀹炵幇濮旀墭缁欎簡鍙︿竴涓?被锛屼袱涓?被缁勫悎璧锋潵瑙e喅闂??锛屸?滃?鐢ㄧ粍鍚?灏戠敤缁ф壙鈥?/span>

鏍囧噯瀹炵幇

鍩虹被锛?

public abstract class Base {     protect Behavior beh;     public Base() {     }     protect void setBehavior(Behavior beh) {          this.beh = ben;     }     public void performBehavior() {          if (beh != null) {               beh.doIt();          }     }}

瀛愮被

public class Child extends Base {          public Child() {          Behavior beh = new Behavior1();          setBehavior(h);     }}

濮旀墭鎺ュ彛锛?

public interface Behavior {     public void doIt();}

濮旀墭鎺ュ彛瀹炵幇1

public class Behavior1 implements Behavior {     public void doIt() {          //鍏蜂綋瀹炵幇     }}

android搴旂敤瀹炰緥
google Volley
Volley涓?殑姣忎釜缃戠粶璇锋眰琚?皝瑁呮垚浜嗕竴涓?被锛屼粬浠?叡鍚岀户鎵胯嚜鍩虹被Request锛孯equest鐨勫瓙绫绘湁JsonObjectRequest锛?ImageRequest绛夈?備粬浠?兘闇?瑕佷竴涓?綉缁滈噸杩炵殑绠楁硶銆?/span>
鎵?浠?oogle鍐欎簡涓?涓?繖涓?畻娉曠殑鎺ュ彛

/** * Retry policy for a request. */public interface RetryPolicy {    /**     * Returns the current timeout (used for logging).     */    public int getCurrentTimeout();    /**     * Returns the current retry count (used for logging).     */    public int getCurrentRetryCount();    /**     * Prepares for the next retry by applying a backoff to the timeout.     * @param error The error code of the last attempt.     * @throws VolleyError In the event that the retry could not be performed (for example if we     * ran out of attempts), the passed in error is thrown.     */    public void retry(VolleyError error) throws VolleyError;}

瀹冪殑瀹炵幇鏈塂efaultRetryPolicy绛?/span>


杩欓噷鏄?釜鍗佸垎鏍囧噯鐨勭瓥鐣ユā寮?/span>
鍦≧equest鍩虹被涓?湁涓?涓?垚鍛樺彉閲?/span>
聽聽

  private RetryPolicy mRetryPolicy;


鍚屾椂涔熸湁涓?涓猻et鏂规硶

    /**     * Sets the retry policy for this request.     *     * @return This Request object to allow for chaining.     */    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {        mRetryPolicy = retryPolicy;        return this;    }

聽鐒跺悗鍦ㄥ垵濮嬪寲Request鐨勬椂鍊欒皟鐢ㄤ簡杩欎釜set鏂规硶锛屾妸閫傚綋鐨勯噸鑱旂畻娉曡?閰嶈繘鍘?/span>

    public Request(int method, String url, Response.ErrorListener listener) {        mMethod = method;        mUrl = url;        mErrorListener = listener;        setRetryPolicy(new DefaultRetryPolicy());  //杩欎釜鏄?熀绫伙紝鍥犳?鏀句簡涓?涓?粯璁ょ殑鏂规硶杩涘幓        mDefaultTrafficStatsTag = TextUtils.isEmpty(url) ? 0: Uri.parse(url).getHost().hashCode();    }               public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,            Config decodeConfig, Response.ErrorListener errorListener) {        super(Method.GET, url, errorListener);        setRetryPolicy(                new DefaultRetryPolicy(IMAGE_TIMEOUT_MS, IMAGE_MAX_RETRIES, IMAGE_BACKOFF_MULT)); //杩欎釜鏄?瓙绫伙紝涓哄叾閫夋嫨浜嗙壒娈婄殑閲嶈仈绠楁硶        mListener = listener;        mDecodeConfig = decodeConfig;        mMaxWidth = maxWidth;        mMaxHeight = maxHeight;    }

  相关解决方案