我正在研究通過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. 如何通過服務器上的XML文件更新數據
- 2. 從其他服務器上的數據庫更新數據庫
- 3. 通過服務器上的數據庫循環,並更新數據
- 4. 如何自動更新數據庫服務器上的mysql數據庫更改?
- 5. 如何通過服務或報警管理器更新數據到數據庫
- 6. 從Glassfish服務器上的FoxPro數據更新sql數據庫
- 7. 當服務器數據庫更新時更新sqlite數據庫
- 8. 如何通過API訪問JasperReports服務器的數據庫?
- 9. 通過JSON更新iPhone SQLite數據庫
- 10. 如何更新我的Windows服務器上的數據庫?
- 11. 如何通過IP連接到服務器上的數據庫
- 12. 如何更新服務器數據庫上的utc時間戳
- 13. 更新Sqlite數據庫通過雲服務器
- 14. 從服務器MySql數據庫更新本地服務器MySql數據庫
- 15. SQL服務器:通過主數據庫
- 16. 如何通過jquery更新數據庫
- 17. 如何將數據庫數據發送到MySQL服務器以更新服務器數據庫
- 18. 通過Android上的服務從數據庫檢索JSON
- 19. 使用json通過服務器數據庫的折線圖
- 20. 從多個本地數據庫更新服務器上的數據庫
- 21. YUI數據表未通過服務器中的數據刷新
- 22. JQGrid刷新數據從服務器ater更新數據編輯
- 23. 檢查服務器上的新數據
- 24. JqGrid從服務器更新數據
- 25. 如何通過api在android中向服務器發送數據?
- 26. 如何在更新服務器上的數據後從WCF服務更新客戶端中的數據?
- 27. 通過彈出服務器發佈的數據更新TableView
- 28. 通過服務獲取JSON數據
- 29. 通過JavaScript發送JSON數據到服務器通過JavaScript
- 30. Sqlite數據庫不會更新來自Web服務的數據