在nsurlconnection中传递参数Post方法

时间:2016-04-29 08:09:56

标签: ios uitableview nsurlconnection uipickerview

screenshot for my coding我厌倦了挑选者视图,其中包含来自网址链接的四个元素数组" branch_name"

(1)带按钮动作。

按钮操作:

- (IBAction)picker_button:(id)sender {
    NSString *parseURL =@"url(1)";
    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"];
        NSLog(@"%@",json);
        arrmsg1 =[json valueForKeyPath:@"Branches.id"];
               [_pickerdata1 reloadAllComponents];
        }

}

然后我使用url link(2)使用POST NSURLConnection传递参数并成功显示在tableview中。

发布方法编码: 这里我传递参数为Branchid = @" 1"但我想将参数作为整个branchid值传递。帮助我

-(void) sendDataToServer : (NSString *) method{
    NSString *Branchid =@"1" ;

    NSURL *url = nil;
    NSMutableURLRequest *request = nil;
    if([method isEqualToString:@"GET"]){

        NSString *getURL = [NSString stringWithFormat:@"%@?branch_id=%@", 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];
    }

}

连接完成加载:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError* error;
    json = [NSJSONSerialization JSONObjectWithData: mutableData
                                           options:kNilOptions
                                             error:&error];
    NSLog(@"%@",json);
    sellArray = [json valueForKeyPath:@"BranchByList.sell"];
    buyArray = [json valueForKeyPath:@"BranchByList.buy"];
    updataedArray = [json valueForKeyPath:@"BranchByList.updated"];
    imageArray = [json valueForKeyPath:@"BranchByList.flag_image"];
    currency =[json valueForKeyPath:@"BranchByList.currency"];
    [ _dataview reloadData];
    }

0 个答案:

没有答案
相关问题