<ServiceContract()> _
Public Interface IGetEmployees
<OperationContract()> _
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json,BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="json/contactoptions/?strCustomerID={strCustomerID}")> _
Function GetAllContactsMethod(strCustomerID As String) As List(Of NContactNames)
End Interface
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function GetAllContactsMethod(strCustomerID As String) As List(Of NContactNames) Implements IGetEmployees.GetAllContactsMethod
Utilities.log("Hit get all contacts at 56")
Dim intCustomerID As Integer = Convert.ToInt32(strCustomerID)
Dim lstContactNames As New List(Of NContactNames)
'I add some contacts to the list.
Utilities.log("returning the lst count of " & lstContactNames.Count)
Return lstContactNames
End Function
所以当我编写上面的代码并在浏览器中调用它时,就像http://xyz-dev.com/GetEmployees.svc/json/contactoptions/?strCustomerID=123一样,我得到10行作为JSON格式的结果。这就是我的意图。但是当我从客观c方面打电话时,它会像这样抛出异常
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
我的目标c代码是:
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");
NSJSONSerialization行发生异常。 所以这是我的问题的延续Web service method not hit when called via Objective C我改变了我的代码,所以我发布了一个新问题。这是我在asp方面编写uritemplate的正确方法吗?这是我在iOS方面调用的正确方法吗?如果您需要更多信息,请告诉我。感谢..
答案 0 :(得分:1)
您的网址似乎不正确。确保它是正确的。
您需要按照NSURLConnectionDelegate设置此服务。这是我经常重用的一些示例代码。您需要设置连接,然后正确处理数据。我已经创建了一个委托,并在完成或错误时通知。
离。
#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];
}
}