0

id、lat、lng、timestampをsqliteに保存しているので、JSONオブジェクトでサーバーに送信する必要があります。

ここにデータを挿入します。

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);

そして、これが私が今取り組んでいるコードです。ここからどこに行けばいいのかわかりません:

 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) {

これがデータの送信に使用したいJSON/HTTPコードです

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

どんな助けでもいただければ幸いです!

4

1 に答える 1

1

私は同じコードを使用していますが、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];
于 2013-04-19T04:10:55.023 に答える