1
<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 端调用的正确方式吗?如果您需要更多信息,请告诉我。谢谢..

4

1 回答 1

1

您的网址似乎不正确。确保它是正确的。

您需要按照 NSURLConnectionDelegate 来设置此服务。这是我经常重复使用的一些示例代码。您需要设置连接,然后正确处理您的数据。我创建了一个委托并在完成或错误时通知。

文档:http: //developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

前任。

#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];
    }
}
于 2013-01-03T19:32:14.033 回答