2013-02-02 37 views
0

在我的應用程序中,我使用SBJson解析值。網絡服務以點網開發。使用PHP的Web服務的代碼工作得很好,但點網絡Web服務,這不顯示響應值。代碼中沒有錯誤或異常或警告。如何從dot net webservices檢索給出json輸出的值?

這是web服務的JSON輸出:

{ 「頭」:[{ 「ID」: 「0」, 「MSG」: 「用戶名或密碼不匹配」}]}

的用戶名密碼是錯在這裏,但我的代碼,甚至沒有任何返回錯誤的值。

這一個是我的代碼:

NSString *post = [NSString stringWithFormat:@"key=%@&username=%@&password=%@",key.text,[txtUsername text],[txtPassword text]]; 
NSLog(@"PostData: %@",post); 

NSURL *url = [NSURL URLWithString:@"http://jabdcdfddfsd.aspx"]; 

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSLog(@"%@", postData); 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
NSLog(@"%@",postLength); 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSLog(@"%@",request); 
NSError *error = [[NSError alloc] init]; 
NSHTTPURLResponse *response = nil; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSLog(@"%@",response); 

NSLog(@"Response code: %d", [response statusCode]); 
if ([response statusCode] >=200 && [response statusCode] <300) 
{ 
    NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Response ==> %@", responseData) 

幫助我。 responseData不打印任何內容。在我的代碼中有任何問題,或者問題在於我的意思是在dotnet Web服務中。

+0

的urlData不是nil和urlData.length> 0並且它NSUTF8StringEncoding編碼? –

回答

1

我認爲,如果代碼工作正常使用舊的PHP Web服務,你應該看看你的.Net Web服務很長的很難看。在某些方面它很可能是不同的。嘗試使用類似Chrome Advanced REST client的內容來查看每個web服務如何響應。

+0

非常感謝你的回答@Stig Brautaset,但我沒有看到在這個Chrome高級REST中看到的東西 – Atul

+0

放入你從你的應用程序調用的URL。將您的兩項服務的輸出與相同的輸入進行比較。他們是一樣的嗎? –

+0

'( { ID = 0; 味精=「用戶名或密碼不匹配」; } )'你能告訴我請我怎樣才能從這個數組中檢索「ID」的價值 – Atul