【问题标题】:Afnetworking request get time out on iPhone 5 , iPhone 6 and iPhone 6+ but works on iPhone 4Afnetworking 请求在 iPhone 5、iPhone 6 和 iPhone 6+ 上超时,但在 iPhone 4 上有效
【发布时间】:2015-03-20 07:36:35
【问题描述】:

我有以下网址:

http://beta.bemembr.com/en/webservice/abc123/members/login/email/******/password/****

还有这段代码:

 NSString *path = [NSString stringWithFormat:@"en/webservice/%@/members/login/email/%@/password/%@",apiKey,login,pass];
    NSLog(@"PATH IS %@",path);
    [manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
    {
        NSDictionary *json = (NSDictionary *)responseObject;
        NSLog(@"JSON IS %@",json);
        int status = [[json valueForKey:@"status"] intValue];
        if(status == 200){
            compblock(YES);
        }else{
            compblock(NO);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error)
    {
         // Failure

         NSLog(@"Failure: %@", error);
        compblock(NO);
    }];

这在 iPhone4 上有效,但在 iPhone 5、iPhone6 和 iPhone 6+ 上失败

这是我得到的错误:

Failure: Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x7f8951c5c7b0 {NSErrorFailingURLStringKey=http://beta.bemembr.com/en/webservice/abc123/members/login/email/*****/password/*****, NSUnderlyingError=0x7f8951c82220 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)", NSErrorFailingURLKey=http://beta.bemembr.com/en/webservice/abc123/members/login/email/*****/password/*****}

谁能帮帮我?

谢谢!

【问题讨论】:

  • iPhone 4运行的是什么iOS版本,你的其他设备运行的是什么iOS版本?我遇到了一些问题,iOS 8 现在对网络请求比以前的版本更“挑剔”(有关可能性,请参阅下面的答案)。

标签: ios objective-c iphone afnetworking nsurlrequest


【解决方案1】:

尽量避免 URL 中的用户名和密码部分,并尝试使用类似的方法。

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com]];

[client setAuthorizationHeaderWithUsername:@"username" password:@"password"];

【讨论】:

    【解决方案2】:

    使用 AFNetworking:[AFNetworking2.0]https://github.com/AFNetworking/AFNetworking

    UIKit+AFNetworkingAFNetworking 文件夹添加到您的项目中。

    并将此代码添加到您要使用 AFNetworking 的文件中:

    #import "AFnetworking.h"
    #import "UIKit+AFNetworking.h"
    

    并使用此代码:

    -(void)login
    {    
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
        [manager POST:@"some_url" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
        {
            NSLog(@"JSON: %@", responseObject);
        } 
        failure:^(AFHTTPRequestOperation *operation, NSError *error) 
        {
            NSLog(@"Error: %@", error);
       }];
    }
    

    由于您在 URL 字符串中传递参数,请将其设置为 nil:parameters:nil

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      相关资源
      最近更新 更多