/
HttpCacheDemo.java
79 lines (68 loc) · 3.14 KB
/
HttpCacheDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cn.trinea.android.demo;
import java.util.Date;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import cn.trinea.android.common.entity.HttpResponse;
import cn.trinea.android.common.service.HttpCache;
import cn.trinea.android.common.service.HttpCache.HttpCacheListener;
import cn.trinea.android.common.util.CacheManager;
import cn.trinea.android.common.util.StringUtils;
/**
* HttpCacheDemo
*
* @author <a href="http://www.trinea.cn/android/android-http-cache/" target="_blank">Trinea</a> 2013-11-18
*/
public class HttpCacheDemo extends BaseActivity {
public static final String TAG_CACHE = "http_cache";
private EditText httpUrlET;
private Button httpGetBT;
private TextView httpGetContentTV;
private TextView httpCacheInfoTV;
private HttpCache httpCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, R.layout.http_cache_demo);
// get the singleton instance of HttpCache
httpCache = CacheManager.getHttpCache(context);
// or create a new HttpCache, like this:
// httpCache = new HttpCache(context);
httpUrlET = (EditText)findViewById(R.id.http_cache_url);
httpGetBT = (Button)findViewById(R.id.http_cache_get);
httpGetContentTV = (TextView)findViewById(R.id.http_cache_content);
httpCacheInfoTV = (TextView)findViewById(R.id.http_cache_info);
httpGetBT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = httpUrlET.getText().toString();
url = StringUtils.isEmpty(url) ? httpUrlET.getHint().toString() : url;
httpCache.httpGet(url, new HttpCacheListener() {
protected void onPreGet() {
httpCacheInfoTV.setText("");
httpGetContentTV.setText("wating…");
httpGetBT.setEnabled(false);
}
protected void onPostGet(HttpResponse httpResponse, boolean isInCache) {
if (httpResponse != null) {
StringBuilder sb = new StringBuilder(256);
sb.append("is in cache: ").append(isInCache).append("\r\n");
if (isInCache) {
sb.append("expires: ").append(new Date(httpResponse.getExpiredTime()).toGMTString())
.append("\r\n");
}
httpCacheInfoTV.setText(sb.toString());
httpGetContentTV.setText(httpResponse.getResponseBody());
} else {
httpCacheInfoTV.setText("");
httpGetContentTV.setText("response is null.");
}
httpGetBT.setEnabled(true);
}
});
}
});
}
}