我想从我的服务器获取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"];
}
}
}];
}
哪个版本AFNetworking是否在使用?你可以发布响应输出吗? – Hussein
响应: - 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 = –
responseString = { DataUpdation =“”; Employee =“”; MyApprovalList =“”; ProjectList =“”; Status = { StatusCode = 700; StatusMessage =“凭证无效,请检查您的凭证”; }; TimeSheetList =“”; } –