【问题标题】:<NSMutableURLRequest: 0x1c0c7ae0> { URL: (null) } in Objective C<NSMutableURLRequest: 0x1c0c7ae0> { URL: (null) } 在 Objective C
【发布时间】:2017-11-21 08:56:21
【问题描述】:

以下代码出现问题

-(void)AddEmpService:(NSDictionary *)params
{

    recordResults = FALSE;
    OutPutStringName = [[NSMutableString alloc] init];
    [OutPutStringName appendString: @"string"];

    NSString *urlstr =[NSString stringWithFormat:@"http://www.seen.com/SeenApi/Service.svc/EmpService?type=Insert&Id=&ClassId=%@&Name=%@&Desg=%@&Age=%@&Add=%@&Sal=%@&Gend=%@&Phone=%@&EmpId=%@",[params objectForKey:@"Id"],[params objectForKey:@"EmpName"],[params objectForKey:@"EmpDesignation"],[params objectForKey:@"EmpAge"],[params objectForKey:@"EmpAddress"],[params objectForKey:@"EmpSalary"],[params objectForKey:@"EmpGender"],[params objectForKey:@"EmpPhone"],[params objectForKey:@"EmpId"]];

    NSLog(@"dict is %@",params);
    urlstr=[urlstr stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSLog(@"%@",urlstr);
    NSURL *url=    [NSURL URLWithString:urlstr];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSLog(@"the request %@",theRequest);
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if( theConnection )
    {
        webData = [NSMutableData data];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

}

NSLog(@"%@",urlstr);是

http://www.seen.com/SeenApi/Service.svc/EmpService?type=Insert&Id=&ClassId=63&Name=Mark&Desg=Teacher&Age=25&Add=New%%20York&Sal=5000&Gend=Male&Phone=0987654321&EmpId=235

但是得到 null 为 NSLog(@"请求 %@",theRequest);

the request <NSMutableURLRequest: 0x1c0c7ae0> { URL: (null) }
2017-11-21 14:07:36.262 Emp[7070:2394869] ERROR with theConenction

请帮我找出问题。TIA

【问题讨论】:

  • 字符串中有两个连续的% 字符。强烈建议使用NSURLComponentsNSURLQueryItem 来编写URL。它隐式处理百分比编码。
  • 能否请您更正字符串@vadian
  • 我不能,因为我不知道字典的确切内容。但最好使用stringByAddingPercentEncodingWithAllowedCharacters 来添加正确的百分比编码,而不是stringByReplacingOccurrencesOfString

标签: ios objective-c iphone json xcode


【解决方案1】:

试试这个:

NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[urlstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 2015-06-20
    • 1970-01-01
    相关资源
    最近更新 更多