ios - <NSMutableURLRequest : 0x1c0c7ae0> { URL: (null) } in Objective C

标签 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.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

但是获取空值 NSLog(@"请求 %@",theRequest);

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

请帮我找出问题所在。TIA

最佳答案

尝试使用这个:

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

关于ios - <NSMutableURLRequest : 0x1c0c7ae0> { URL: (null) } in Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47408964/

相关文章:

ios - 使UIImage具有圆角和边框

c++ - Objective-c iOS 中的二维码生成器

ios - 快照方法在 iPhone 6 设备和模拟器上损坏

ios - 收到推送通知

IOS devicesWithMediaType 弃用

iphone - 将 Interface Builder 中创建的 View 添加到以编程方式创建的 View 中 - 可以吗?

ios - 防止这种与可选相关的崩溃的最佳方法是什么?

objective-c - 为 Linux 编译 Objective-C 应用程序(API 覆盖率)

iphone - 加载到 map 中,地址簿中单个人的多个地址

c++ - 在 Cocoa iPhone App 中使用 C++ 文件