2013-01-03 65 views
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面把它拋出這樣從調用Objective C的一個asp.net web服務以JSON格式參數

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 

我的目標C代碼的例外是:發生在NSJSONSerialization線

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"); 

例外。 所以這是我的問題的延續Web service method not hit when called via Objective C我改變了我的代碼,所以我發佈了一個新問題。這是我寫在asp端uritemplate的正確方法嗎?是我在iOS端調用的正確方式嗎?如果您需要更多信息,請告訴我。謝謝..

+0

是'jUrlString'是否正確? – mkral

+0

請NSLog的網址,並檢查它是否正確 –

+0

@MidhunMP我改變了我的代碼上面。我添加了NSLog,並在contactoptions後添加了'/',它的輸出如下https://xyz-dev.com/GetEmployees.svc/JSON/contactoptions /?strCustomerID = 123。我將它粘貼到我的瀏覽器中,並輸出10條記錄。 – RookieAppler

回答

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]; 
    } 
} 
+1

你應該檢查AFNetworking .... – mkral

+0

會做。大多數日子裏,我爲上面構建的每個服務設置了一個類似於上述代碼的基類和子類,通常我只需要更改數據解析。 – propstm

+0

@propstm。謝謝。我有兩個問題.1。你合成的代理類型(你的代碼的第3行)和2.什麼是? – RookieAppler