1818
1919package com .loopj .android .http ;
2020
21+ import android .content .Context ;
22+ import android .net .http .HttpResponseCache ;
2123import android .os .Handler ;
2224import android .os .Looper ;
2325import android .os .Message ;
2931import org .apache .http .util .EntityUtils ;
3032
3133import java .io .IOException ;
34+ import java .io .UnsupportedEncodingException ;
35+ import java .security .NoSuchAlgorithmException ;
3236
3337/**
3438 * Used to intercept and handle the responses from requests made using
@@ -72,6 +76,10 @@ public class AsyncHttpResponseHandler {
7276 protected static final int FINISH_MESSAGE = 3 ;
7377
7478 private Handler handler ;
79+
80+ private Context mContext ;
81+ private Boolean mCacheRequest = false ;
82+ private String mURL ;
7583
7684 /**
7785 * Creates a new AsyncHttpResponseHandler
@@ -87,7 +95,24 @@ public void handleMessage(Message msg){
8795 }
8896 }
8997
90-
98+ public AsyncHttpResponseHandler (Context context ,boolean cacheRequest ) {
99+ this ();
100+ mContext = context ;
101+ mCacheRequest =cacheRequest ;
102+ }
103+
104+ public Context getAppContex (){
105+ return mContext ;
106+ }
107+
108+ public boolean isForcedToReadFromCacheEnabled (){
109+ return mCacheRequest ;
110+ }
111+
112+ public void setUrl (String url ){
113+ mURL = url ;
114+ }
115+
91116 //
92117 // Callbacks to be overridden, typically anonymously
93118 //
@@ -106,15 +131,18 @@ public void onFinish() {}
106131 * Fired when a request returns successfully, override to handle in your own code
107132 * @param content the body of the HTTP response from the server
108133 */
109- public void onSuccess (String content ) {}
134+ public void onSuccess (String content ) {
135+
136+ }
110137
111138 /**
112139 * Fired when a request returns successfully, override to handle in your own code
113140 * @param statusCode the status code of the response
114141 * @param content the body of the HTTP response from the server
115142 */
116- public void onSuccess (int statusCode , String content ) {
117- onSuccess (content );
143+ public void onSuccess (int statusCode , String content ,boolean isFromCache ) {
144+
145+ onSuccess (content );
118146 }
119147
120148 /**
@@ -164,8 +192,10 @@ protected void sendFinishMessage() {
164192 // Pre-processing of messages (in original calling thread, typically the UI thread)
165193 //
166194
167- protected void handleSuccessMessage (int statusCode , String responseBody ) {
168- onSuccess (statusCode , responseBody );
195+
196+ protected void handleSuccessMessage (int statusCode , String responseBody ,boolean isFromCache ) {
197+
198+ onSuccess (statusCode , responseBody ,isFromCache );
169199 }
170200
171201 protected void handleFailureMessage (Throwable e , String responseBody ) {
@@ -181,7 +211,12 @@ protected void handleMessage(Message msg) {
181211 switch (msg .what ) {
182212 case SUCCESS_MESSAGE :
183213 response = (Object [])msg .obj ;
184- handleSuccessMessage (((Integer ) response [0 ]).intValue (), (String ) response [1 ]);
214+ try {
215+ CacheManager .cacheData (mContext , ((String ) response [1 ]).getBytes () ,CacheManager .SHA1 (mURL ));
216+ } catch (Exception e ) {
217+ e .printStackTrace ();
218+ }
219+ handleSuccessMessage (((Integer ) response [0 ]).intValue (), (String ) response [1 ],false );
185220 break ;
186221 case FAILURE_MESSAGE :
187222 response = (Object [])msg .obj ;
0 commit comments