1
嗨,在我的應用程序中,我已將數據存儲在本地數據庫sqlite3中。現在我必須將數據表格本地數據庫sqlite3上傳到在線服務器。爲了檢索數據,我使用了dataDictionary來獲取sqlite3數據庫的所有數據。而且我已經將數據轉換爲json格式,現在的問題是如何將這個json數據傳遞到我的在線服務器。如何將JSON數據對象POST到iOS7中的服務器
NSMutableArray *array = [[NSMutableArray alloc] init];
[self openDB];
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM reg"];
const char *query_stmt = [sql UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, nil)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];
NSString *date = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
field1Str= date;
NSString *customer = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
field2Str = customer;
NSString *code1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)];
field3Str = code1;
NSString *code2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,3)];
field4Str = code2;
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field1Str] forKey:@"Kname"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field2Str] forKey:@"kphone"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field3Str] forKey:@"karea"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",field4Str] forKey:@"kcity"];
[array addObject:_dataDictionary];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:array options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@"my url for inserting the data into my online server"];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
// print json:
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding]);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
}
我已經使用上面的代碼將我的sqlite3數據庫數據傳遞到我的服務器。我已經打印jsondata記錄其即將作爲json格式數據。請告訴我如何將這些數據推送到我的在線服務器,在那裏我錯過了代碼,我在這裏呆了很長一段時間,它吃掉我的頭不工作,請幫我。
謝謝。
上面的代碼工作正常,但其存儲在我的數據庫空值,請 – user3427551