0

我正在一个应用程序中工作,我需要json在请求字符串的该参数中传递对象,现在我被困在这里,不知道该怎么做。

 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 *urlLoc=@"";
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);
4

1 回答 1

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 *urlLoc=@"";
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);

Connection的委托方法

 - (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)
}

/ /// / ///////// 已编辑答案 ///////////////////

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];
于 2013-07-10T12:22:27.667 回答