1 package com.lonshin.chexiaodi.http;
2
3 import java.util.Date;
4 import java.util.List;
5 import java.util.Set;
6 import java.util.Timer;
7 import java.util.TimerTask;
8 import java.util.TreeSet;
9
10 import org.apache.http.NameValuePair;
11 import org.apache.http.message.BasicNameValuePair;
12
13 import com.krislq.sliding.MyApplication;
14
15 import android.content.Context;
16 import android.content.ContextWrapper;
17 import android.content.SharedPreferences;
18 import android.util.Log;
19 /**
20 * to check if the local JSONResult exist
21 *
22 * and do the write and read
23 * @author linsen
24 *
25 */
26
27
28 public class SPTool {
29 SharedPreferences sp;
30 SharedPreferences.Editor editor;
31
32 private SPTool(){
33 sp = MyApplication.getInstance().getSharedPreferences("Result", Context.MODE_WORLD_READABLE);
34 editor = sp.edit();
35
36
37 }
38
39 private static SPTool instance = new SPTool(){};
40
41 public static SPTool getInstance(){
42 return instance;
43 }
44 /**
45 * 判断是否有本地json 而且是没过期的
46 *
47 * @return
48 */
49 public boolean hasSDResult(List<NameValuePair> params){
50 // Log.d("httplocal-in Sdjr",String.valueOf(sp.contains(params.toString())));
51 long oldT = 0;
52 long newT = 0;
53
54 //取得保存的String当时的时间oldT
55 if(sp.contains(params.toString())){
56 String s = sp.getString(params.toString(), null);
57 if(s.contains("#")){
58 String[] str = s.split("#");
59 oldT = Long.parseLong(str[0]);
60 newT = new Date().getTime();
61 }
62 }
63
64 //设置过期时间为1小时---若测试用则设为3秒
65 if(((newT-oldT)<5000)&&((newT-oldT)>0)){
66 return true;
67 }else{
68 editor.remove(params.toString());
69 return false;
70 }
71
72
73 }
74
75 /**
76 * 访问结果访问到本地
77 * @param params
78 * @param result
79 */
80 public void saveToSD(List<NameValuePair> params , String result){
81
82 //在保存的String中增加一个time及符号"#"
83 String time = String.valueOf(new Date().getTime());
84 String result01 = time+"#"+result;
85 editor.putString(params.toString(), result01);
86 editor.commit();
87 Log.d("httplocal-in Sdjr","saveToSD"+params.toString());
88 }
89
90 /**
91 * 获取本地保存的访问结果
92 * @param params
93 * @return
94 */
95 public String getSDResult(List<NameValuePair> params){
96 Log.d("httplocal" , "getSDResult");
97 String s = sp.getString(params.toString(), null);
98
99 //取得数据时,去掉附加的time信息
100 if(s.contains("#")){
101 String[] str = s.split("#");
102
103 return str[1];
104 }else{
105 return s;
106 }
107 }
108 }