我正在研究通過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. 如何從服務器獲取數據並使用它更新數據庫?