我想從我的服務器獲取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 =「」; } –