Objective c 从objective c调用asp.net Web服务以返回带有参数的JSON格式的数据
我的目标c代码是:Objective c 从objective c调用asp.net Web服务以返回带有参数的JSON格式的数据,objective-c,ios,web-services,exception,parameters,Objective C,Ios,Web Services,Exception,Parameters,我的目标c代码是: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' NSJSONSerialization行发生异常。 这是我问题的延续,我稍微修改了我的代码,所以我发布了一个新问题。这是我在asp端编写uritemplate的正确方式吗?这是我在iOS端调用uritemplate的正确方式吗?如果你需要更多信息,请告诉我。谢谢。您的u
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
NSJSONSerialization行发生异常。
这是我问题的延续,我稍微修改了我的代码,所以我发布了一个新问题。这是我在asp端编写uritemplate的正确方式吗?这是我在iOS端调用uritemplate的正确方式吗?如果你需要更多信息,请告诉我。谢谢。您的url似乎不正确。确保它是正确的 您需要按照NSURLConnectionLegate设置此服务。下面是我经常重用的一些示例代码。您需要设置连接,然后正确处理数据。我已经创建了一个委托,并在完成或出错时通知 文件: 前
#定义kBgQueue调度(获取)全局(调度(队列)优先级(默认值为0)
@JSONService的实现
@综合代表;
-(无效)开始{
调度异步(kBgQueue^{
n错误*错误=nil;
NSURL*NSURL=[NSURL URLWithString:[NSString stringWithFormat:@“%@?strustomeId=%@,@”https://xyz-dev.com/GetEmployees.svc/json/contactoptions“,strustomerid]];
NSData*data=[NSData data WITH CONTENTS OFURL:nsURL options:NSDATARREADINGUNCACHED error:&error];
如果(错误){
NSLog(@“%@,[错误本地化描述]);
[self-notifyDelegateOfError:错误];
}否则{
NSLog(@“数据已成功加载”);
}
[self-performSelectorOnMainThread:@selector(processData:)withObject:data waitUntilDone:YES];
});
}
-(作废)取消{
//TODO KILL该服务(优雅地!!!!!)--允许VC取消该服务并防止故障
}
-(id)initWithDelegate:(id)代理
{
self=[super init];
如果(自我){
[自行设定代理人:代理人];
}
回归自我;
}
-(void)processData:(NSData*)数据{
//解析出json数据
n错误*错误;
如果(数据==nil){
错误=[N错误errorWithDomain:@“无域”代码:001用户信息:无];
[self-notifyDelegateOfError:错误];
返回;
}
//NSDictionary=json或NSMutableArray=json
NSDictionary*json=[NSJSONSerialization JSONObject WithData:data
选项:针织品
错误:&错误];
//NSArray*dataArray=[[json objectForKey:@“数据”]objectForKey:@“当前条件”];
//…在这里进行更多解析。
//没有错误,全部完成!
[自我通知完成委托人];
}
-(无效)notifyDelegateOfError:(N错误*)错误{
[delegate webService:self-didFailWithError:error];
}
-(无效)通知被授权人完成
{
if([delegate respondsToSelector:@selector(WebServiceIDComplete:)])){
[代理webServiceDidComplete:self];
}
}
您的url似乎不正确。确保它是正确的
您需要按照NSURLConnectionLegate设置此服务。下面是我经常重用的一些示例代码。您需要设置连接,然后正确处理数据。我已经创建了一个委托,并在完成或出错时通知
文件:
前
#定义kBgQueue调度(获取)全局(调度(队列)优先级(默认值为0)
@JSONService的实现
@综合代表;
-(无效)开始{
调度异步(kBgQueue^{
n错误*错误=nil;
NSURL*NSURL=[NSURL URLWithString:[NSString stringWithFormat:@“%@?strustomeId=%@,@”https://xyz-dev.com/GetEmployees.svc/json/contactoptions“,strustomerid]];
NSData*data=[NSData data WITH CONTENTS OFURL:nsURL options:NSDATARREADINGUNCACHED error:&error];
如果(错误){
NSLog(@“%@,[错误本地化描述]);
[self-notifyDelegateOfError:错误];
}否则{
NSLog(@“数据已成功加载”);
}
[self-performSelectorOnMainThread:@selector(processData:)withObject:data waitUntilDone:YES];
});
}
-(作废)取消{
//TODO KILL该服务(优雅地!!!!!)--允许VC取消该服务并防止故障
}
-(id)initWithDelegate:(id)代理
{
self=[super init];
如果(自我){
[自行设定代理人:代理人];
}
回归自我;
}
-(void)processData:(NSData*)数据{
//解析出json数据
n错误*错误;
如果(数据==nil){
错误=[N错误errorWithDomain:@“无域”代码:001用户信息:无];
[self-notifyDelegateOfError:错误];
返回;
}
//NSDictionary=json或NSMutableArray=json
NSDictionary*json=[NSJSONSerialization JSONObject WithData:data
选项:针织品
错误:&错误];
//NSArray*dataArray=[[json objectForKey:@“数据”]objectForKey:@“当前条件”];
//…在这里进行更多解析。
//没有错误,全部完成!
[自我通知完成委托人];
}
-(无效)notifyDelegateOfError:(N错误*)错误{
[delegate webService:self-didFailWithError:error];
}
-(无效)通知被授权人完成
{
if([delegate respondsToSelector:@selector(WebServiceIDComplete:)])){
[代理webServiceDidComplete:self];
}
}
jUrlString是否正确?请记录url并检查是否正确correct@MidhunMP我更改了上面的代码。我添加了NSLog,在contactoptions之后添加了“/”,输出如下。我把它粘贴到我的浏览器中,它会输出10条记录。@Phanindar:这是准确的url吗?我在上面的url上得到错误@对不起。不是。我必须用客户公司名称替换xyz。但是我想说的是,URL是有效的,它给了我10条记录。jUrlString是否正确?请记录URL并检查它是否正确correct@MidhunMP我更改了上面的代码。我添加了NSLog,在contactoptions之后添加了“/”,输出如下。我将其粘贴到浏览器中,然后输出
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
NSString *strCustomerID = [NSString stringWithFormat:@"%i",123];
jUrlString = [NSString stringWithFormat:@"%@?strCustomerID=%@",@"https://xyz-dev.com/GetEmployees.svc/json/contactoptions/",strCustomerID];
NSLog(@"the jurlstring is %@",jUrlString);
NSURL *jurl = [NSURL URLWithString:jUrlString];
NSError *jError;
NSData *jData = [NSData dataWithContentsOfURL:jurl];
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:jData options:kNilOptions error:&jError];
NSLog(@"%@",json);
NSLog(@"Done");
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
@implementation JSONService
@synthesize delegate;
- (void)start{
dispatch_async(kBgQueue, ^{
NSError *error = nil;
NSURL *nsURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@?strCustomerID=%@",@"https://xyz-dev.com/GetEmployees.svc/json/contactoptions",strCustomerID]];
NSData* data = [NSData dataWithContentsOfURL:nsURL options:NSDataReadingUncached error:&error];
if (error) {
NSLog(@"%@", [error localizedDescription]);
[self notifyDelegateOfError:error];
} else {
NSLog(@"Data has loaded successfully.");
}
[self performSelectorOnMainThread:@selector(processData:) withObject:data waitUntilDone:YES];
});
}
- (void)cancel{
//TODO KILL THE SERVICE (GRACEFULLY!!!!!) -- ALLOW VC'S TO CANCEL THE SERVICE & PREVENT SEGFAULTS
}
- (id)initWithDelegate:(id<WebServiceDelegate>)aDelegate
{
self = [super init];
if (self) {
[self setDelegate:aDelegate];
}
return self;
}
- (void)processData:(NSData *)data{
//parse out the json data
NSError* error;
if(data == nil){
error = [NSError errorWithDomain:@"NO_DOMAIN" code:001 userInfo:nil];
[self notifyDelegateOfError:error];
return;
}
//EITHER NSDictionary = json or NSMutableArray = json
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
//NSArray *dataArray = [[json objectForKey:@"data"] objectForKey:@"current_condition"];
//... more parsing done here.
//NO ERRORS ALL DONE!
[self notifyDelegateOfCompletion];
}
- (void)notifyDelegateOfError:(NSError *)error{
[delegate webService:self didFailWithError: error];
}
- (void)notifyDelegateOfCompletion
{
if ([delegate respondsToSelector:@selector(webServiceDidComplete:)]) {
[delegate webServiceDidComplete:self];
}
}