【问题标题】:How to use GET REST method using with AFNetworking in Objective C如何在Objective C中使用与AFNetworking一起使用的GET REST方法
【发布时间】: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


【解决方案1】:

将 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]);
}];

【讨论】:

  • 我将 AFNetworking 更新到 3.0 版,但仍然出现同样的错误 { DataUpdation = "";员工 = ""; MyApprovalList = ""; ProjectList = "";状态 = { 状态代码 = 700; StatusMessage = "凭据无效,请检查您的凭据"; }; TimeSheetList = ""; }
  • 这似乎是服务器端的问题,请检查您必须发送哪些参数以及是否正确发送了它们。表示您发送的凭据有误,请再次检查并告诉我会发生什么
  • 感谢 Hussein 我已经与后端进行了交叉检查,并在 Rest 客户端上检查了此参数和基本密钥是否正确。请检查我的代码以及 GET 方法 n 告诉我我错在哪里?请复制我的代码 n 测试一次并告诉我请
  • 我刚刚检查了您的代码(并尝试过),请求返回 JSON,但我确定发送的参数或服务器端有问题。再次检查或发布一些代码,以便我们为您提供帮助
  • 能否请您发送您的电子邮件 ID 或 Skype ID 我将分享我的代码 n 文档详细信息相同的参数 n 链接在 Android 中工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
相关资源
最近更新 更多