2014-03-19 32 views
0

嗨在我的應用程序中,我已經將數據存儲在本地數據庫sqlite3中。現在我必須將數據表格本地數據庫sqlite3上傳到在線服務器。爲了檢索數據,我使用了dataDictionary來獲取sqlite3數據庫的所有數據。將nsmutable數組數據傳遞給NSURLRequest:發佈數據IOS7

在我的h文件我有我的網址插入數據到我這樣的在線服務器。

#define kPostURL @"url" 
    #define Kname @"name" 
    #define kphone @"phone" 
    #define karea @"area" 
    #define kcity @"city" 

在此以下代碼我已存儲在數據字典中的所有的數據,我已經存儲的數據到一個名爲陣列nsmutable陣列。如果我在日誌中打印數組,顯示所有數據。現在的問題是如何使用post方法將這個可管理的數據傳遞到在線服務器。

NSMutableArray *array = [[NSMutableArray alloc] init]; 
    entries = [[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; 

     //NSString *str = [[NSString alloc]initWithFormat:@"%@ - %@ - %@ - %@",field1Str,field2Str,field3Str,field4Str]; 
     //[entries addObject:str]; 

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

在這下面的代碼我傳遞使用POST方法,該數據已經在.m文件聲明在線服務器中的數據,但我不如何在POST方法的NSMutableArray。

-(void) withName:(NSString *) na phone:(NSString *) ph are:(NSString *) arr cit:(NSString *) citt { 


    if (na!= nil && ph != nil && arr != nil && citt != nil) { 

     NSMutableString *postString = [NSMutableString stringWithString:kPostURL]; 

     [postString appendString:[NSString stringWithFormat:@"?%@=%@", Kname,na]]; 
     [postString appendString:[NSString stringWithFormat:@"&%@=%@", kphone,ph]]; 
     [postString appendString:[NSString stringWithFormat:@"&%@=%@", karea,arr]]; 
     [postString appendString:[NSString stringWithFormat:@"&%@=%@", kcity,citt]]; 



     [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

     NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]]; 
     [request setHTTPMethod:@"POST"]; 

     postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

    } 
    } 

在上面的代碼中,我通過點擊按鈕傳遞數據。請告訴如何讓它長時間滯留在我的頭上。請給我一些解決方案。

- (IBAction)send:(id)sender { 
     [self withName:field1Str phone:field2Str are:field3Str cit:field4Str]; 
    } 

謝謝。

回答

1

這取決於服務器接受的URL格式是什麼。

如果服務器支持它,最簡單的答案也是最差的答案(不太適合未來的方案),就是使用如下URL:?kname[0]=x&kname[1]=y來傳遞兩個條目。

但是,如果您想同時發佈大量數據,我會建議使用請求的正文,而不是URL,因爲您很快就會達到最大網址長度。

所以你應該首先設計你的服務器來接受一組記錄,然後在客戶端使用服務器端定義的格式。通常,一個好的解決方案是使用NSJSONSerialization序列化客戶端的字典數組,將JSON字符串設置爲請求的主體,並解析JSON服務器端。

此外,您應該在將每個字符串添加到您的URL之前轉義每個字符串。否則,你會如何區分&分隔你的字段和出現在你的字符串中的任何&?

+0

感謝你的回放真的你的回答對我來說更有用我想嘗試一下這個方法你的答案然後我會回到這裏 – user3427551

0

你必須先了解你的web服務需要什麼參數。爲了將數據發送到服務器,您可以根據數組內容創建XML結構並將其傳遞給webservice參數。 你可以像下面

<record> 
<name>value</name> 
<city>value</city> 
. 
. 
</record> 

創建XML,重複設置的所需時間紀錄。

+0

我想要我的web服務的字符串im使用url來傳遞這個數據請告訴如何將這個nsmutable數據傳遞到post方法 – user3427551

+0

如果超過了3-4個參數,那麼最好的做法是創建XML結構並使一個肥皂信封。 –

+0

我從來沒有使用xml結構和soap信封傳遞數據之前可以給你一些想法或有任何教程的學習 – user3427551