首先在本地初始化一个数据库
- (void)initDatabase
{
//最终数据库路径
NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"];
NSFileManager *fm = [NSFileManager defaultManager];
if(![fm fileExistsAtPath:dbPath])
{
EGODatabase* database = [EGODatabase databaseWithPath:dbPath];
strSql = @"creat table if not exists notice(id integer primary key asc,noticeid text,title text, image text,content text,timestamp text);";
[database executeQuery:strSql];
[database close];
}
然后保存信息在本地数据库,因为我做的时资讯,所以有一个判断,如果本地已经有这个资讯则直接返回,如果有更新资讯就插入最新的资讯
-(void)saveNotice:(NoticeMessage *)notice
{
NSString *dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"];
EGODatabase* database = [EGODatabase databaseWithPath:dbPath];
//查询是否已经存在,如果存在就直接返回不插入
NSString *sqlQuery = [[NSString alloc]initWithFormat:@"select count(*) from notice where noticeid = %@",notice.strnotId];
//NSLog(@"%@",sqlQuery);
EGODatabaseResult *result = [database executeQuery:sqlQuery];
for(EGODatabaseRow* row in result) {
NSString* strTotal = [row stringForColumnAtIndex:0];
if ([strTotal integerValue] > 0) {
return;
}
}
NSString* sqlInsert = [[NSString alloc] initWithFormat:@"insert into notice(noticeid,title,date,image,content,timestamp) values(‘%@‘,‘%@‘,‘%@‘,‘%@‘,‘%@‘,‘%@‘)",notice.strNoticeId,notice.strTitle,notice.strDate,notice.strImage,notice.strContent,notice.strTimeStamp];
[database executeQuery:sqlInsert];
NSLog(@"save message:%@",sqlInsert);
[database close];
}
我们在与服务器端同事沟通时做了一个资讯的id,通过ID判断是否最新资讯,所以会查询一下本地数据库里的id,那么我就抽出了这个数据库方法,将这个id赋给一个全局变量,
在把这个全局变量赋给后面的请求。
//检查本地公告id
-(void)searchNoticeDataBase
{
EGODatabase* database = [EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
//查询是否已经存在最新公告
NSString *sqlQuery = [[NSString alloc]initWithFormat:@"select noticeid from notice group by category order by noticeid limit 1"];
//NSLog(@"%@",sqlQuery);
EGODatabaseResult *result = [database executeQuery:sqlQuery];
for(EGODatabaseRow* row in result)
{
NoticeMessage *msg = [[NoticeMessage alloc]init];
msg.strnotId = [row stringForColumn:@"noticeid"];
noticeId = msg.strnotId;
}
[database close];
}
请求的方法
-(void)checkNewNotice
{
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString* strToken = [pref stringForKey:@"logintoken"];
NSString *oldNoticeId = noticeId;
if (oldNoticeId == nil) {
oldNoticeId = @"0";
}
//向管理平台登录,获取token
NSString *strUrl = [NSString stringWithFormat:@"http://xxx.xx.x.xxx:xxxx/fxxxxxxxx?xxxx=%@&xxxx=%@",strToken,oldNoticeId];
NSLog(@"Chect new info %@",strUrl);
NSURL *url = [NSURL URLWithString:strUrl];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(@"login result:%@",responseString);
NSError *error = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSString* msgCode = [dict objectForKey:@"code"];
if ([msgCode isEqualToString:@"200"]) {
NSLog(@"get new info success!");
NSDictionary *noticeDict = [dict objectForKey:@"result"];
for (NSDictionary* dResult in noticeDict) {
NSString *strNoticeId = [dResult objectForKey:@"noticeid"];
NSString *strTitle = [dResult objectForKey:@"title"];
NSString *strDate = [dResult objectForKey:@"date"];
NSString *strImage = [dResult objectForKey:@"image"];
NSString *strContent = [dResult objectForKey:@"content"];
NSString *strTimeStamp = [dResult objectForKey:@"timestamp"];
NoticeMessage *notice = [[NoticeMessage alloc]init];
notice.strNoticeId = strNoticeId;
notice.strTitle = strTitle;
notice.strDate = strDate;
notice.strImage = strImage;
notice.strContent = strContent;
notice.strTimeStamp = strTimeStamp;
[self saveNotice:notice];
}
//重新加载资讯信息
[self loadNotice];
[self.tableValued reloadData];
// [SVProgressHUD dismiss];
}
else if([msgCode isEqualToString:@"404"]) {
NSLog(@"找不到用户");
// [SVProgressHUD showErrorWithStatus:@"用户名或密码有误"];
return;
}
else{
;// [SVProgressHUD dismiss];
}
NSLog(@"msgCode is %@",msgCode);
// Use when fetching binary data
//NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"login result:%@",error.description);
}];
[request startAsynchronous];
}
还需要一个从数据库载入的方法
- (void)loadNotice
{
if (!arrayNotice) {
arrayNotice = [[NSMutableArray alloc]init];
}
EGODatabase* database = [EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
//查询所有的公告
NSString *sqlQuery = [[NSString alloc]initWithFormat:@"select distinct * from notice order by timestamp"];
//NSLog(@"%@",sqlQuery);
EGODatabaseResult *result = [database executeQuery:sqlQuery];
for(EGODatabaseRow* row in result) {
NoticeMessage *msg = [[NoticeMessage alloc]init];
msg.strNoticeId = [row stringForColumn:@"noticeid"];
msg.strTitle = [row stringForColumn:@"title"];
msg.strContent = [row stringForColumn:@"content"];
msg.strTimeStamp = [row stringForColumn:@"timestamp"];
[arrayNotice insertObject:msg atIndex:0];
}
[database close];
}
那么最后,我是在登录的时候调用请求的方法
[self checkNewNotice]
说实话我还是个小菜鸟,这个项目真的帮助我提升了许多,虽然很多还是不懂,但我都觉得我已经是高富帅了
xmPP(即时通讯)向远程服务器请求数据,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/tuhaoYY/p/3850894.html