Ios <;NSMutableURLRequest:0x1c0c7ae0>;目标C中的{URL:(null)}
下面的代码有问题Ios <;NSMutableURLRequest:0x1c0c7ae0>;目标C中的{URL:(null)},ios,objective-c,iphone,json,xcode,Ios,Objective C,Iphone,Json,Xcode,下面的代码有问题 -(void)AddEmpService:(NSDictionary *)params { recordResults = FALSE; OutPutStringName = [[NSMutableString alloc] init]; [OutPutStringName appendString: @"string"]; NSString *urlstr =[NSString stringWithFormat:@"http://www.s
-(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);是
但是得到空值
NSLog(@“请求%@”,请求)
请求{URL:(null)}
2017-11-21 14:07:36.262电磁脉冲[7070:2394869]与组件错误
请帮助我找出问题所在。请尝试使用以下方法:
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[urlstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];
字符串中有两个连续的
%
字符。强烈建议使用NSURLComponents
和NSURLQueryItem
组合URL。它隐式处理百分比编码。请您更正字符串@vadianI can't,因为我不知道字典的确切内容。但是,最好使用stringByAddingPercentEncodingWithAllowedCharacters
来添加正确的百分比编码,而不是stringByReplacingOccurrencesOfString
。
the request <NSMutableURLRequest: 0x1c0c7ae0> { URL: (null) }
2017-11-21 14:07:36.262 Emp[7070:2394869] ERROR with theConenction
NSMutableURLRequest * theRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[urlstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]];