如何通过json API来更新服务器数据库上的数据?

我正在从iPhone通过json / rest api从服务器检索sqlite数据库的iPhone应用程序。 用户可以将行添加到本地表中,并可以在本地进行更新。 现在,当我添加了一些行到本地数据库中的表,我想同步/插入这些新的行到我的本地更新数据库的服务器数据库。 请帮助,如果有人知道该API方法(JSON /rest)或如果有任何相关的教程请帮助。

当你说你正在检索“sqlite”数据库,你的意思是所有的表和他们的行的“JSON”表示? 我假设你实际上并没有发送“sqlite”数据库文件。

对于通过http发送和检索json,为简单起见,可以使用NSURLConnection和NSURLRequest,因为它们是内置的。如果要强制执行到核心数据的映射,可以将RestKit框架用于连接和数据处理。

以下是前一种解决scheme的示例实现 – 假设您是ARC,否则您需要添加适当的保留和发布语句。

1)声明你正在使用的类作为合适的代表

@interface ClassName : NSObject <NSURLConnectionDelegate> 

2)声明一个将被用来接收数据的responseData对象

  //interface @property (nonatomic, strong) NSMutableData *responseData; //implementation @synthesize responseData; 

3)创build发送json请求的函数

  - (void)sendRequest { responseData = [NSMutableData data]; //whatever your server address is NSURL *url = [NSURL URLWithString:@"http://www.resturl.com/whatever"]; //just sample data - create this dictionary with what you want to send NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; [params setObject:@"SomeValue" forKey:@"SomeKey"]; NSError *jsonError; //NSJSONSerialization is Apple's new json serialization class so we can use it to convert to and from json and foundation objects NSData *requestdata = [NSJSONSerialization dataWithJSONObject:params options:0 error:&jsonError]; NSMutableURLRequest *request; request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:requestdata]; //this kicks off the request asynchronously NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //if you'd rather send a synchronous request, you can use the static NSURLConnection function //sendSynchronousRequest:returningResponse:error: } 

4)实现委托function来接收我们的数据

 //any time a piece of data is received we will append it to the responseData object - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [self.responseData appendData:data]; } //some sort of error, you can print the error or put in some other handling here, possibly even try again but you will risk an infinite loop then unless you impose some sort of limit - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { // Clear the activeDownload property to allow later attempts self.responseData = nil; } //connection has finished, thse requestData object should contain the entirety of the response at this point - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSError *jsonError; NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError]; if(responseDict) { NSLog(@"%@", responseDict); } else { NSLog(@"%@", [jsonError description]); } //clear out our response buffer for future requests self.responseData = nil; } 

如果您想使用一些新的信息来更新远程数据库,只需在本地跟踪新行(而不是仅将它们与完整数据集合并),并将只包含这些行的新请求发送到将添加它们的端点。 这是在不执行实际映射的情况下执行此操作的最简单方法。