2012-03-01 63 views
2

我正在製作一個iPhone應用程序,它需要我使用網站表單來獲取數據庫中的值,並且可以通過我的iPhone應用程序以JSON格式進行查詢。在iphone應用程序中的表格

現在我想,因爲我的網站形式只有5-6個字段填滿,我可以在我的iphone應用程序本身做一個表單嗎?然後,我可以將這些表單數據以JSON格式發送到網絡服務器。 有人請指導我。

回答

1

您可以通過post方法將數據發送給jason。

-(void)registration:(NSString *)nickName 
    TeamName:(NSString *)teamName 
    Age:(NSString *)age 
    Nationality:(NSString *)countryName 

{ 
NSString *JSON = [NSString stringWithFormat:@"{\"NICKNAME\":\"%@\"",nickName]; 
JSON = [JSON stringByAppendingFormat:@",\"TEAM_NAME\":\"%@\"",teamName]; 
JSON = [JSON stringByAppendingFormat:@",\"AGE\":\"%@\"",age]; 
JSON = [JSON stringByAppendingFormat:@",\"NATIONALITY\":\"%@\"",countryName]; 
NSLog(@"%@", JSON); 

    NSData *postData = [JSON dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];  
    NSString *strURL = [NSString stringWithFormat:@"%@%@", kServerPath, kUserRegisterPath]; 

NSLog(@"Resgistration URL: %@", strURL); 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:120.0];  

[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setHTTPBody:postData]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:[WebServices sharedInstance]]; 


if(theConnection) { 
    NSLog(@"connection made"); 
} 

else { 
    NSLog(@"theConnection is NULL"); 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error " message:@"Network not responding" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

} 

http://iosdevelopertips.com/networking/iphone-json-flickr-tutorial-part-1.html

iPhone/iOS JSON parsing tutorial

這些鏈接可以幫助你