【问题标题】:how to convert nsaaray into nsstring using nsurlconnection Post method如何使用 nsurlconnection Post 方法将 nsaaray 转换为 nsstring
【发布时间】: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


    【解决方案1】:

    尝试像这样创建一个“POST”方法:-

    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
                                                Branchid, @"branch_id", nil];
    NSString *newRequest = [dict JSONRepresentation];
    
    NSData *requestData = [newRequest dataUsingEncoding:NSUTF8StringEncoding];
    
    //If you are not using SBJsonParser Library then try the following:
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    
    NSString *urlString = @"YOUR URL String";
    NSURL *requestURL = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long) [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:requestData];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (connection) {
        mutableData = [NSMutableData new];
    }
    

    另外,如果您没有找到正常工作的答案,您可以点击此链接:- Data in POST or GET methods

    Send data in POST methods

    【讨论】:

    • 显示 json 表示错误没有可见界面 @Nirmit Dagly
    • 你使用过 SBJsonPaeser 库吗?如果没有,请注释掉该行,然后重试。
    • 不,如果我评论 tat 行也显示错误,我没有使用 SBJsonParser
    • 你能把你得到的错误贴出来吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2012-02-10
    • 2013-07-24
    • 2011-04-17
    • 2010-11-20
    相关资源
    最近更新 更多