在我的應用程序中,我使用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服務中。
的urlData不是nil和urlData.length> 0並且它NSUTF8StringEncoding編碼? –