2013-05-14 61 views
-2
NSString *username = @"user"; 
NSString *password = @"password"; 
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; 
[dictionnary setObject:username forKey:@"user_email"]; 
[dictionnary setObject:password forKey:@"user_password"]; 

NSLog(@".....%@....",dictionnary); 

NSError *error = nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary 
                options:kNilOptions 
                error:&error]; 

NSString *urlString = @"http://abcd.com/SVCs/WSUserService.svc/MobSignIn"; 

NSURL *url = [NSURL URLWithString:urlString]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 

[request setHTTPBody:jsonData]; 
NSURLResponse *response = NULL; 
NSError *requestError = NULL; 
NSData *responseData = [NSURLConnection 
         sendSynchronousRequest:request 
         returningResponse:&response 
         error:&requestError]; 
NSString *responseString = [[NSString alloc] 
          initWithData:responseData 
          encoding:NSASCIIStringEncoding] ; 
NSLog(@"%@", responseString); 

我想張貼對Web服務的用戶名和密碼JSON對象的問題..但它給出了一個模糊的輸出..任何人都可以幫助我在此我已經張貼JSON對象

Output: 

2013-05-14 18:50:17.155 UWUI[6226:11303] .....{ 
"user_email" = user; 
"user_password" = password; 
}.... 

2013年5月14日18:50:18.233 UWUI [6226:11303] **

後跟一個XML格式的內容

**

回答

3
NSString *UN = @"user"; 
NSString *PWD = @"password"; 
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; 
[dictionnary setObject:UN forKey:@"UN"]; 
[dictionnary setObject:PWD forKey:@"PWD"]; 

NSLog(@"dictionnary...%@", dictionnary); 

NSError *error = nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary 
                options:kNilOptions 
                 error:&error]; 


NSString *urlString = @"http://abcd.com/SVCs/WSUserService.svc/MobSignIn"; 

NSURL *url = [NSURL URLWithString:urlString]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:jsonData]; 
NSURLResponse *response = NULL; 
NSError *requestError = NULL; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError]; 
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ; 
NSLog(@"%@", responseString); 
1

設置內容類型可能會解決您的問題。發送之前給定的代碼添加到您的要求,

[request addValue:@"application/json" forHTTPHeaderField:@"Content-type"]; 
+0

沒有朋友。不過我得到了「responseString」的XML輸出.. – Mano 2013-05-15 04:20:25