【发布时间】:2016-05-02 05:07:19
【问题描述】:
我正在使用两个链接。我为“id”创建了 nsaaray 的第一个链接。我需要使用 POST 作为参数在 nsurlconnection 中传递这个“id”通过第二个链接的方法。我尝试了很多,但我在传递参数“id”时卡住了。
id 的 nsaaray:
- (IBAction)button_drop:(id)sender {
NSString *parseURL =@"first_link";
NSString *encodeurl =[parseURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodeurl];
NSData *data = [NSData dataWithContentsOfURL:url];
if(data){
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
arrMsg = [json valueForKeyPath:@"Branches.branch_name"];
arrmsg1 =[json valueForKeyPath:@"Branches.id"];
[_picker2 reloadAllComponents];
}
}
使用 nsurlconnection 的 POST 方法:
NSString *Branchid=@"3";
NSURL *url = nil;
NSMutableURLRequest *request = nil;
if([method isEqualToString:@"GET"]){
NSString *getURL = [NSString stringWithFormat:@"%@?branch_id=%@\n", URL, Branchid];
url = [NSURL URLWithString: getURL];
request = [NSMutableURLRequest requestWithURL:url];
NSLog(@"%@",getURL);
}else{ // POST
NSString *parameter = [NSString stringWithFormat:@"branch_id=%@",Branchid];
NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
url = [NSURL URLWithString: URL];
NSLog(@"%@", parameterData);
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:parameterData];
arr= [NSMutableString stringWithUTF8String:[parameterData bytes]];
}
[request setHTTPMethod:method];
[request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if( connection )
{
mutableData = [NSMutableData new];
}
【问题讨论】:
标签: ios json http-post nsurlconnection