2016-07-22 60 views
1

我想从我的服务器获取JSON,但我需要将一些参数传递给URL。我不想将这些参数添加到URL,因为它们包含Content-Type,DeviceType,DeviceID和Authorization。如何使用GET REST方法与AFNetworking一起使用Objective C

对于一个简单的URL请求我有下面的代码,但响应为空,请告诉我这段代码有什么问题?

Constant.h

#define KSERVER_URL @"http://101.127.236.85:6067/tmsservice/MobileService.svc/" 

#define KAppInfo_URL @"AuthenticateEmployee" 

-(void)loginRequestContentType:(NSString*) content deviceType:(NSString*) deviceT deviceID:(NSString*) devId Authorization:(NSString*) auth WithResponse: (void (^)(id responseObject, NSError *error))responseAndErr { 
NSString *urlString = [NSString stringWithFormat:@"%@%@",KSERVER_URL,KAppInfo_URL]; 

NSLog(@"URL = %@", urlString); 
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 

[parameters setValue:@"application/json" forKey:@"Content-Type"]; 
[parameters setValue:@"iOS" forKey:@"DeviceType"]; 
[parameters setValue:@"123456" forKey:@"DeviceID"]; 
[parameters setValue:strGlobalfinalString forKey:@"Authorization"]; 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 

[manager GET:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    responseAndErr(responseObject, Nil); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    responseAndErr(Nil, error); 
}]; 

}

Login.m

-(void)sendLoginRequest { 
    MBProgressHUD *progressIndicator = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; 
    progressIndicator.labelText = kLoadingTitle; 
    progressIndicator.detailsLabelText = kPleaseWaitText; 

    NSString *userEmail = self.userNameTextField.text; 
    NSString *userPassword = self.passwordTextField.text; 

    NSLog(@"Email = %@", userEmail); 
    NSLog(@"Password = %@", userPassword); 

    NSString *singleString = [NSString stringWithFormat: @"%@:%@", userEmail,userPassword]; 
    NSLog(@" Login Single String = %@", singleString); 

    NSData *nsdata = [singleString dataUsingEncoding:NSUTF8StringEncoding]; 
    // Get NSString from NSData object in Base64 
    NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0]; 

    // Print the Base64 encoded string 
    NSLog(@"Login Encoded: %@", base64Encoded); 


    strGlobalfinalString= [NSString stringWithFormat:@"Basic %@",base64Encoded]; 


    NSLog(@"Login Final Encoded String: %@", strGlobalfinalString); 

    [[NetwokManager sharedInstance] loginRequestContentType:@"application/json" deviceType:@"iOS" deviceID:@"123456" Authorization:strGlobalfinalString WithResponse:^(id responseObject, NSError *error) 

    { 
     [progressIndicator hide:YES]; 

     if (!error) { 

      NSLog(@"responseString = %@", responseObject); 
      HomeViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; 
      [self.navigationController pushViewController:controller animated:YES]; 
      if (responseObject == nil) 
      { 
       NSLog(@"No data from server"); 
       [self showAlertViewWithMessage:@"No data downloaded from server!"]; 
      } 

      NSDictionary *resposneDict = (NSDictionary*)responseObject; 
      NSLog(@"New Dictionary Data = %@", resposneDict); 


      NSString *msg; 
      msg = [resposneDict objectForKey:@"status"]; 
      if ([msg isEqualToString:@"success"]) 
      { 


       HomeViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; 
       [self.navigationController pushViewController:controller animated:YES]; 


      } 
      else if ([resposneDict objectForKey:@"StatusMessage"]) 
      { 
       [self showAlertViewWithMessage:@"Invalid credentials, please check your credentials"]; 
      } 
     } 
    }]; 
} 
+0

哪个版本AFNetworking是否在使用?你可以发布响应输出吗? – Hussein

+0

响应: - URL = http://101.127.236.85:6067/tmsservice/MobileService.svc/AuthenticateEmployee 电子邮件= [email protected] 密码= P @ ssw0rd 网络单串=安吉。 [email protected]:P @ ssw0rd 编码的字符串:基本YW5qaS5jQHRvdGFsZWJpenNvbHV0aW9ucy5jb206UEBzc3cwcmQ = 内容类型=应用/ JSON 器件类型= iOS的 设备ID = 123456 Authontication =基本YW5qaS5jQHRvdGFsZWJpenNvbHV0aW9ucy5jb206UEBzc3cwcmQ = –

+0

responseString = { DataUpdation =“”; Employee =“”; MyApprovalList =“”; ProjectList =“”; Status = { StatusCode = 700; StatusMessage =“凭证无效,请检查您的凭证”; }; TimeSheetList =“”; } –

回答

0

更新AFNetworking到3.0或更高版本尝试以下操作:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
NSDictionary *parameters = @{@"userid":userId, @"x-session": session}; 
NSString *urlString = @"http://api.example.com/sampleLink"; 
[manager GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
    NSLog(@"%@", responseObject); 

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
    NSLog(@"%@", [error localizedDescription]); 
}]; 
+0

我将AFNetworking更新为版本3.0,但仍然是相同的错误 DataUpdation =“”; Employee =“”; MyApprovalList =“”; ProjectList =“”;状态= { StatusCode = 700; StatusMessage =“凭证无效,请检查您的凭证”; }; TimeSheetList =“”; } –

+0

如何解决这个问题? –

+0

这似乎是一个服务器端问题,请检查您必须发送的参数以及是否已正确发送它们。这是说,你发送错误的凭据,再次检查它们,让我知道会发生什么 – Hussein