2013-02-27 150 views
1

我想和POST方法的請求之後,我希望他們能夠返回JSON數據或NSDictionary,但他們只能返回時,它成功的字符串AFNetworking POST無法返回JSON數據

NSString *response = [operation responseString];

任何人都知道如何讓他們返回NSdictionary而不是NSString

-(IBAction)SubmitLogin:(id)sender{ 
    NSLog(@"Login"); 
    // NSLog(@"%@",username); 

    //START FUNGSI UNTUK POST, PUT, DELETE 

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:signinUrl]; 
    [httpClient defaultValueForHeader:@"Accept"]; 



    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"someuser", @"username", 
          @"somepassword",@"password", 
          nil]; 

    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
                  path:@"" 
                 parameters:params]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

    [operation setCompletionBlockWithSuccess: 
    ^(AFHTTPRequestOperation *operation, 
     id responseObject) { 
     NSString *response = [operation responseString]; 
     NSLog(@"response: %@",response); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"error: %@", [operation error]); 
    }]; 


    //call start on your request operation 
    [operation start]; 

回答

0

這是我所用,隨意它適合你的需求:

-(void) loginWithUserID:(NSString*)usrID User:(NSString*)usr Password:(NSString *)psw Sender:(id)sender 
{ 
    if ([sender respondsToSelector:@selector(startLoading)]){ 
     [sender performSelector:@selector(startLoading)]; 
    } 

    baseURL = [NSString stringWithFormat: 
        @"http://xxx.xxx.xxx.xxx/test/service.svc/weblogin"]; 
    NSString* serviceURL = [NSString stringWithFormat: 
          @"%@?userID=%@&user=%@&password=%@",baseURL,usrID,usr,psw; 

    NSURL *url = [NSURL URLWithString:[serviceURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     self.LoginOK = [JSON valueForKeyPath:@"LoginOK"]; 
     if (LoginOK.intValue == 1) { 
      NSDictionary *usrData = [JSON valueForKeyPath:@"userData"]; 
      userData = [[UserData alloc]init]; 
      [userData readFromJSONDictionary:usrData]; 
      NSLog(@"Userdata: %@",userData); 
      if ([sender respondsToSelector:@selector(loginSuccessful:)]) 
      { 
       [sender performSelector:@selector(loginSuccessful:)]; 
      } 
     }else{ 
      UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Correct Login Credentials" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
      [alertView show]; 
      ;} 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     if ([sender respondsToSelector:@selector(stopLoading)]){ 
      [sender performSelector:@selector(stopLoading)]; 
     } 
     [[[UIAlertView alloc] initWithTitle:@"Error" 
            message:@"Loginservice not available! Check your connection!" 
            delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles: nil] show]; 
     NSLog(@"Error: %@", error); 

    }]; 

    [operation start]; 
    }; 

編輯:哦,使用

-(void)readFromJSONDictionary:(NSDictionary *)d 
{ 

    [self setProperty1:[d objectForKey:@"property1"]]; 
} 

方法來獲得所有屬性出來的將該字典轉換爲自定義類。

+0

謝謝你的回答和編輯我的問題,我的英語不好對不起。 – Zoro 2013-02-27 10:51:59

+0

我沒有編輯你的問題Zoro,也不擔心你的英語,這是非常可以理解的。希望這一點的代碼可以幫助你一點,歡迎來到Stack Overflow :)。 – 2013-02-27 10:53:28

1

我試圖發送JSON對象並取回JSON數據並分析它

NSURL *url = [NSURL URLWithString:@"https://MySite.com/"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url]; 
//[httpClient setParameterEncoding:AFJSONParameterEncoding]; 
//[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
         @"Ans", @"name", 
         @"29", @"age", nil]; 
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" 
                 path:@"/testJSONReturn.php" 
                parameters:params]; 
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    NSLog(@"DATA: %@", [JSON valueForKeyPath:@"data"]); 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    NSLog(@"Request Failure Because %@",[error userInfo]); 
}]; 

[operation start]; 

可能有助於