如何通过json api更新服务器数据库的数据?[英] How to update data on server db through json api?

本文是小编为大家收集整理的关于如何通过json api更新服务器数据库的数据?的处理方法,想解了如何通过json api更新服务器数据库的数据?的问题怎么解决?如何通过json api更新服务器数据库的数据?问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我正在开发一个 iPhone 应用程序,它通过 json/rest api 从服务器检索 sqlite 数据库.并且用户可以在本地向其表中添加行并可以在本地更新它.现在,当我在本地数据库中的表中添加了一些行时,我只想将那些新行从本地更新的数据库同步/插入到服务器数据库.如果有人知道该 api 方法(json/rest),请提供帮助,或者如果有任何相关的教程,请提供帮助.

推荐答案

当您说您正在检索"sqlite"数据库时,您是指所有表及其行的"json"表示吗?我假设您实际上并没有发送"sqlite"数据库文件.

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

这是前一个解决方案的示例实现 - 它假设您是 ARC,否则您需要添加适当的保留和释放语句.

1) 将您正在使用的类声明为适当的委托

    @interface ClassName : NSObject <NSURLConnectionDelegate>

2) 声明一个用于接收数据的 responseData 对象

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

    //implementation
    @synthesize responseData;

3) 创建发送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;
    }

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

本文地址:https://www.itbaoku.cn/post/924584.html