我正在研究通过json/rest api从服务器检索sqlite数据库的iPhone应用程序。用户可以将行添加到本地表中,并可以在本地进行更新。现在,当我在本地数据库的表中添加了一些行时,我想从本地更新的数据库中仅将这些新行同步/插入服务器数据库。 如果有人知道该API方法(json/rest),请帮忙,或者如果有任何相关的教程,请帮助。如何通过json api更新服务器数据库上的数据?
1
A
回答
3
当你说你正在检索“sqlite”数据库时,你的意思是所有表及其行的“json”表示吗?我假设你实际上并没有发送“sqlite”数据库文件。
对于通过http发送和检索json,为简单起见,可以使用NSURLConnection和NSURLRequest,因为它们是内置的。如果要强制执行映射到核心数据,可以将RestKit框架用于连接和数据处理。
以下是前一种解决方案的示例实现 - 它假定您是ARC,否则您需要添加适当的保留和发布语句。
1)声明您使用的作为适当的委托
@interface ClassName : NSObject <NSURLConnectionDelegate>
2)声明将用来接收数据
//interface
@property (nonatomic, strong) NSMutableData *responseData;
//implementation
@synthesize responseData;
3 responseData对象)创建函数的类发送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)实现委托功能接收我们的数据
//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;
}
如果要更新一些新的信息的远程数据库,只需跟踪新行的地方(而不是仅仅与完整数据集将它们合并),并发送包含只有那些行的新要求到将添加它们的端点。这是在不执行实际映射的情况下执行此操作的最简单方法。
相关问题
- 1. 通过服务器上的数据库循环,并更新数据
- 2. 如何自动更新数据库服务器上的mysql数据库更改?
- 3. 如何通过服务或报警管理器更新数据到数据库
- 4. 当服务器数据库更新时更新sqlite数据库
- 5. 如何通过API访问JasperReports服务器的数据库?
- 6. 如何更新我的Windows服务器上的数据库?
- 7. 如何通过IP连接到服务器上的数据库
- 8. 从服务器MySql数据库更新本地服务器MySql数据库
- 9. SQL服务器:通过主数据库
- 10. 如何将数据库数据发送到MySQL服务器以更新服务器数据库
- 11. 通过Android上的服务从数据库检索JSON
- 12. 从多个本地数据库更新服务器上的数据库
- 13. YUI数据表未通过服务器中的数据刷新
- 14. JqGrid从服务器更新数据
- 15. 如何在更新服务器上的数据后从WCF服务更新客户端中的数据?
- 16. 通过弹出服务器发布的数据更新TableView
- 17. 通过JavaScript发送JSON数据到服务器通过JavaScript
- 18. 通过服务获取JSON数据
- 19. Sqlite数据库不会更新来自Web服务的数据
- 20. 如何通过Web服务编辑/更新数据
- 21. 通过json更新mysql数据
- 22. 如何通过ssh将mysql数据库部署到服务器
- 23. 通过SQL服务器更新WCF数据
- 24. AngularJS通过服务自动更新控制器数据
- 25. 如何通过用户输入将数据插入服务器数据库?
- 26. 如何从gcm服务器响应更新数据库表
- 27. 如何数据库更新网络服务器,在
- 28. 如何从服务器更新数据库
- 29. 如何从服务器获取数据并使用它更新数据库?