Skip to content

Commit 96f210d

Browse files
committed
modify HttpCache.java
1 parent 5aa4a39 commit 96f210d

3 files changed

Lines changed: 123 additions & 7 deletions

File tree

src/cn/trinea/android/common/dao/HttpCacheDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface HttpCacheDao {
1515
* insert HttpResponse
1616
*
1717
* @param httpResponse
18-
* @return
18+
* @return the row ID of the newly inserted row, or -1 if an error occurred
1919
*/
2020
public long insertHttpResponse(HttpResponse httpResponse);
2121

src/cn/trinea/android/common/dao/impl/HttpCacheDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private HttpResponse cursorToHttpResponse(Cursor cursor, String url) {
130130
* @return
131131
*/
132132
private static ContentValues httpResponseToCV(HttpResponse httpResponse) {
133-
if (httpResponse == null) {
133+
if (httpResponse == null || StringUtils.isEmpty(httpResponse.getUrl())) {
134134
return null;
135135
}
136136

src/cn/trinea/android/common/service/HttpCache.java

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class HttpCache extends SimpleCache<String, HttpResponse> {
3030
/** dao to get data from http db cache **/
3131
private HttpCacheDao httpCacheDaoImpl;
3232

33+
private int type = -1;
34+
3335
public HttpCache(Context context){
3436
if (context == null) {
3537
throw new IllegalArgumentException("The context can not be null.");
@@ -45,6 +47,7 @@ public HttpCache(Context context){
4547
*/
4648
public HttpCache(Context context, int type){
4749
this(context);
50+
this.type = type;
4851
initData(type);
4952
}
5053

@@ -60,32 +63,105 @@ private void initData(int type) {
6063
}
6164
}
6265

66+
/**
67+
* http get
68+
* <ul>
69+
* <strong>Attentions:</strong>
70+
* <li>Don't call this on the ui thread, it may costs some times. Becaust if not in cache, it get from network
71+
* synchronous.</li>
72+
* <li>If you want get data asynchronous, use {@link HttpCache#httpGet(HttpRequest, HttpCacheListener)}</li>
73+
* </ul>
74+
*
75+
* @param httpRequest
76+
* @return the response of the url, if null represents http error
77+
*/
6378
public HttpResponse httpGet(HttpRequest request) {
6479
String url;
6580
if (request == null || StringUtils.isEmpty(url = request.getUrl())) {
6681
return null;
6782
}
6883

6984
HttpResponse cacheResponse = getFromCache(url);
70-
return cacheResponse == null ? HttpUtils.httpGet(request) : cacheResponse;
85+
return cacheResponse == null ? putIntoCache(HttpUtils.httpGet(url)) : cacheResponse;
7186
}
7287

88+
/**
89+
* http get
90+
* <ul>
91+
* <li>It gets data from cache or network asynchronous.</li>
92+
* <li>If you want get data synchronous, use {@link HttpCache#httpGet(HttpRequest)} or
93+
* {@link HttpCache#httpGetString(HttpRequest)}</li>
94+
* </ul>
95+
*
96+
* @param httpUrl
97+
* @param listener
98+
*/
7399
public void httpGet(String httpUrl, HttpCacheListener listener) {
74100
new HttpCacheStringAsyncTask(listener).execute(httpUrl);
75101
}
76102

103+
/**
104+
* http get
105+
* <ul>
106+
* <li>It gets data from cache or network asynchronous.</li>
107+
* <li>If you want get data synchronous, use {@link HttpCache#httpGet(HttpRequest)} or
108+
* {@link HttpCache#httpGetString(HttpRequest)}</li>
109+
* </ul>
110+
*
111+
* @param request
112+
* @param listener
113+
*/
77114
public void httpGet(HttpRequest request, HttpCacheListener listener) {
78115
new HttpCacheRequestAsyncTask(listener).execute(request);
79116
}
80117

118+
/**
119+
* http get
120+
* <ul>
121+
* <strong>Attentions:</strong>
122+
* <li>Don't call this on the ui thread, it may costs some times. Becaust if not in cache, it get from network
123+
* synchronous.</li>
124+
* <li>If you want get data asynchronous, use {@link HttpCache#httpGet(HttpRequest, HttpCacheListener)}</li>
125+
* </ul>
126+
*
127+
* @param httpUrl
128+
* @return the response of the url, if null represents http error
129+
*/
81130
public HttpResponse httpGet(String httpUrl) {
82-
HttpResponse cacheResponse = getFromCache(httpUrl);
83-
return cacheResponse == null ? HttpUtils.httpGet(httpUrl) : cacheResponse;
131+
return httpGet(new HttpRequest(httpUrl));
84132
}
85133

134+
/**
135+
* http get
136+
* <ul>
137+
* <strong>Attentions:</strong>
138+
* <li>Don't call this on the ui thread, it may costs some times. Becaust if not in cache, it get from network
139+
* synchronous.</li>
140+
* <li>If you want get data asynchronous, use {@link HttpCache#httpGet(String, HttpCacheListener)}</li>
141+
* </ul>
142+
*
143+
* @param httpUrl
144+
* @return the response body of the url, if null represents http error
145+
*/
86146
public String httpGetString(String httpUrl) {
87-
HttpResponse cacheResponse = getFromCache(httpUrl);
88-
return cacheResponse == null ? HttpUtils.httpGetString(httpUrl) : cacheResponse.getResponseBody();
147+
HttpResponse cacheResponse = httpGet(new HttpRequest(httpUrl));
148+
return cacheResponse == null ? null : cacheResponse.getResponseBody();
149+
}
150+
151+
/**
152+
* http get
153+
* <ul>
154+
* <strong>Attentions:</strong>
155+
* <li>Don't call this on the ui thread, it may costs some times. Becaust if not in cache, it get from network
156+
* synchronous.</li>
157+
* <li>If you want get data asynchronous, use {@link HttpCache#httpGet(HttpRequest, HttpCacheListener)}</li>
158+
* </ul>
159+
*
160+
* @param httpRequest
161+
* @return the response body of the url, if null represents http error
162+
*/
163+
public HttpResponse httpGetString(HttpRequest httpRequest) {
164+
return httpGet(httpRequest);
89165
}
90166

91167
public abstract class HttpCacheListener {
@@ -98,6 +174,46 @@ protected void onPostExecute(HttpResponse httpResponse) {
98174

99175
}
100176

177+
/**
178+
* get type
179+
*
180+
* @return the type
181+
*/
182+
public int getType() {
183+
return type;
184+
}
185+
186+
/**
187+
* set type
188+
*
189+
* @param type the type to set
190+
*/
191+
public void setType(int type) {
192+
this.type = type;
193+
}
194+
195+
/**
196+
* put response into cache
197+
* <ul>
198+
* <li>put response to db, if {@link HttpResponse#getType()} == {@link HttpCache#getType()}, also put into memory
199+
* cache</li>
200+
* </ul>
201+
*
202+
* @param httpResponse
203+
* @return if insert into db error, return null, otherwise return HttpResponse
204+
*/
205+
private HttpResponse putIntoCache(HttpResponse httpResponse) {
206+
String url;
207+
if (httpResponse == null || (url = httpResponse.getUrl()) == null) {
208+
return null;
209+
}
210+
211+
if (type != -1 && type == httpResponse.getType()) {
212+
cache.put(url, httpResponse);
213+
}
214+
return (httpCacheDaoImpl.insertHttpResponse(httpResponse) == -1) ? null : httpResponse;
215+
}
216+
101217
/**
102218
* get from memory cache first, if not exist in memory cache, get from db
103219
*

0 commit comments

Comments
 (0)