package com.lonshin.chexiaodi.http;
import java.util.Date;
import java.util.List;import java.util.Set;import java.util.Timer;import java.util.TimerTask;import java.util.TreeSet;import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;import com.krislq.sliding.MyApplication;
import android.content.Context;
import android.content.ContextWrapper;import android.content.SharedPreferences;import android.util.Log;/** * to check if the local JSONResult exist * * and do the write and read * @author linsen * */ public class SPTool { SharedPreferences sp; SharedPreferences.Editor editor; private SPTool(){ sp = MyApplication.getInstance().getSharedPreferences("Result", Context.MODE_WORLD_READABLE); editor = sp.edit(); } private static SPTool instance = new SPTool(){}; public static SPTool getInstance(){ return instance; } /** * 判断是否有本地json 而且是没过期的 * * @return */ public boolean hasSDResult(List<NameValuePair> params){ // Log.d("httplocal-in Sdjr",String.valueOf(sp.contains(params.toString()))); long oldT = 0; long newT = 0; //取得保存的String当时的时间oldT if(sp.contains(params.toString())){ String s = sp.getString(params.toString(), null); if(s.contains("#")){ String[] str = s.split("#"); oldT = Long.parseLong(str[0]); newT = new Date().getTime(); } } //设置过期时间为1小时---若测试用则设为3秒 if(((newT-oldT)<5000)&&((newT-oldT)>0)){ return true; }else{ return false; }} /** * 访问结果访问到本地 * @param params * @param result */ public void saveToSD(List<NameValuePair> params , String result){ //在保存的String中增加一个time及符号"#" String time = String.valueOf(new Date().getTime()); String result01 = time+"#"+result; editor.putString(params.toString(), result01); editor.commit(); Log.d("httplocal-in Sdjr","saveToSD"+params.toString()); } /** * 获取本地保存的访问结果 * @param params * @return */ public String getSDResult(List<NameValuePair> params){ Log.d("httplocal" , "getSDResult"); String s = sp.getString(params.toString(), null); //取得数据时,去掉附加的time信息 if(s.contains("#")){ String[] str = s.split("#"); return str[1]; }else{ return s; } }
}