如何使用HTTP和JSON将我在sqlite数据库中的数据发送到服务器 [英] How do I send the data I have in my sqlite database to the server with HTTP and JSON

查看:184
本文介绍了如何使用HTTP和JSON将我在sqlite数据库中的数据发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sqlite中存储id,lat,lng和时间戳,现在需要将它在一个JSON对象中发送到服务器。

I'm storing id,lat,lng, and timestamp in sqlite and now need to send it in an JSON object to the server.

这里是我插入数据的位置。

Here's where I'm inserting the data.

NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO location (latitude, longitude, timeStamp) VALUES (%g, %g, %@)",newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp];    
char *error;   
if ( sqlite3_exec(databasehandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
{
    NSLog(@"Location inserted. %@", newLocation.timestamp);
}    

else NSLog(@"Error: %s", error);

这里是我现在工作的代码。我不知道从这里开始:

And here is the code that I'm working on right now. I'm not sure where to go from here:

 NSString *queryStatement = [NSString stringWithFormat:@"SELECT ID, latitude, longitude, timeStamp FROM LOCATION"];

// Prepare the query for execution
sqlite3_stmt *statement;


if (sqlite3_prepare_v2(databasehandle, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
    // Iterate over all returned rows
    while (sqlite3_step(statement) == SQLITE_ROW) {

/ HTTP代码,我要用来发送数据

Here is my JSON/HTTP code that I want to use to send the data

responseData = [NSMutableData data];

//whatever your server address is
NSURL *url = [NSURL URLWithString:@"http://www.resturl.com/whatever"];

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:returnArray 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];

任何帮助将不胜感激。

推荐答案

我使用相同的代码,但不使用webservice在数据库中发布数据。

i am using same code but not post data in database using webservice.

NSURL *url = [NSURL URLWithString:@"someurl/try_ws_iphone/insertRecords.php"];
NSArray *res=[[NSArray alloc] initWithObjects:txtname.text, nil];
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:res 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];

这篇关于如何使用HTTP和JSON将我在sqlite数据库中的数据发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆