【发布时间】:2016-07-22 06:13:48
【问题描述】:
我正在尝试从我的服务器获取 JSON,但我需要将一些参数传递给 url。我不想将这些参数添加到 URL,因为它们包含 Content-Type、DeviceType、DeviceID 和 Authorization。
对于一个简单的 URL 请求,我有以下代码,但响应为空,请告诉我这段代码有什么问题?
常数.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);
}];
}
登录.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?您可以发布响应输出吗?
-
响应:--URL = 101.127.236.85:6067/tmsservice/MobileService.svc/… 电子邮件 = anji.c@totalebizsolutions.com 密码 = P@ssw0rd 网络单个字符串 = anji.c@totalebizsolutions.com:P@ssw0rd 编码字符串:基本YW5qaS5jQHRvdGFsZWJpenNvbHV0aW9ucy5jb206UEBzc3cwcmQ= 内容类型 = 应用程序/json 设备类型 = iOS 设备 ID = 123456 身份验证 = 基本 YW5qaS5jQHRvdGFsZWJpenNvbHV0aW9ucy5jb206UEBzc3cwcmQ=
-
responseString = { DataUpdation = "
";员工 = " "; MyApprovalList = " "; ProjectList = " ";状态 = { 状态代码 = 700; StatusMessage = "凭据无效,请检查您的凭据"; }; TimeSheetList = " "; } -
AFNetworking 版本可能较旧
-
好的!我会更新一次检查
标签: objective-c ios9 afnetworking xcode7.3