2013-07-10 124 views
0

我正在一個應用程序中,我需要通過請求字符串的參數中的json對象,現在我卡在這裏,不知道如何做到這一點。連接如何在php webservice中傳遞參數?

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
    //**check here for responseData & also data** 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    [connection release]; 
    //do something with the json that comes back ... (the fun part) 
} 

////////////// EDITED回答//////////的

SBJSON *json = [SBJSON new]; 
json.humanReadable = YES; 
responseData = [NSMutableData data] ; 

NSString *service = @"http://localhost.abc.com/index.php?p=api/user/register"; 

NSString *requestString = [NSString stringWithFormat:@"{\"Name\":\"%@\",\"Email\":\"%@\",\"Password\":\"%@\",\"PasswordMatch\":\"%@\",\"TermsOfUSe\":\"1\"}",txtusername.text,txtemail.text,txtpassword.text,txtretypepassword.text]; 


NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; 

NSString *[email protected]""; 
urlLoc = [urlLoc stringByAppendingString:service]; 

NSLog(@"URL:- %@",urlLoc); 


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: 
           [NSURL URLWithString: urlLoc]]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; 
[request setHTTPMethod: @"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:requestData]; 

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
NSLog(@"%@",request); 
+0

你可以顯示你已經嘗試了一些代碼,你發送此作爲POST的一部分或GET。你已經創建了JSON或什麼。我們需要更多的細節來幫助 –

+1

http://stackoverflow.com/questions/4456966/how-to-send-json-data-in-the-http-request-using-nsurlrequest – rakeshNS

+1

我做錯了什麼在上面的方法 – Purva

回答

1
SBJSON *json = [SBJSON new]; 
json.humanReadable = YES; 
responseData = [NSMutableData data] ; 

NSString *service = @"http://localhost.abc.com/index.php?p=api/user/register"; 

NSString *requestString = [NSString stringWithFormat:@"{\"Name\":\"%@\",\"Email\":\"%@\",\"Password\":\"%@\",\"PasswordMatch\":\"%@\",\"TermsOfUSe\":\"1\"}",txtusername.text,txtemail.text,txtpassword.text,txtretypepassword.text]; 


NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; 

NSString *[email protected]""; 
urlLoc = [urlLoc stringByAppendingString:service]; 

NSLog(@"URL:- %@",urlLoc); 


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: 
           [NSURL URLWithString: urlLoc]]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; 
[request setHTTPMethod: @"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:requestData]; 

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
NSLog(@"%@",request); 

委派方法///////////

GET方法:在這種方法中,您可以將請求數據追加到web服務的後面。現在行[request setHTTPMethod: @"POST"];

POST方法:在這種方法中,你不能追加請求的數據。但將字典作爲參數傳遞。如下圖所示:

NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"], 
    [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],  nil]; 
NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil]; 
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"]; 

NSString *jsonRequest = [jsonDict JSONRepresentation]; 

NSLog(@"jsonRequest is %@", jsonRequest); 

NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
      cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 


NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; 

[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody: requestData]; 

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
+0

我只想知道我是否提出正確的請求 – Purva

+0

當我做出GET請求時,它給了我正確的響應,但是當我嘗試發佈時沒有問題,但它給了我一個空數組而不是正確的價值 – Purva

+0

是的,當我通過登錄數據它不給我需要輸出,有什麼我需要chenge在做出請求傳遞json字符串 – Purva