【问题标题】:Getting issue of network connection was lost with AFNetworkingAFNetworking 丢失了网络连接问题
【发布时间】:2013-11-04 11:07:06
【问题描述】:

我正在尝试使用 AFNetworking 发送请求,但我遇到了“网络连接丢失”的问题,尽管请求很好。我正在尝试向客户端发送请求,并且得到了正确的响应。

请求的详细信息是: 网址:https://www.ez-point.com/search 方法:获取 授权头:xxxxxxxxxxxxxxx 请求负载:“search_text”:“value”

这是我正在使用的代码:

NSURL *url = [[NSURL alloc] initWithString:@"https://www.ez-point.com/search"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setValue:@"xxxxxxx" forHTTPHeaderField:@"Authorization" ];



    [request setHTTPMethod:@"GET"];

    NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc]init];
    [jsonDic setValue:@"UJO526" forKey:@"search_text"  ];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrinted error:nil];
    [request setHTTPBody:jsonData];


    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request
            success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                NSArray *searchResults = JSON;
                if ([searchResults count] == 1){
                    id result = [searchResults objectAtIndex:0];
                    double latitude = [[result valueForKey:@"latitude"] doubleValue];
                    double longitude = [[result valueForKey:@"longitude"] doubleValue];
                    NSString *ezPoint = [result valueForKey:@"value"];
                    NSString *tags = [result valueForKey:@"tags"];

                    [self setAnnotation:latitude  ForLongitude:longitude withEZPoint:ezPoint WithTags:tags];
                }
            }
            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

            }
     ];
    [operation start];

我得到的错误是:

2013-11-04 15:01:05.143 EZ-POINT[4074:c07] E restkit.network:RKObjectRequestOperation.m:209 GET 'https://www.ez-point.com/search' (0) [2.8806 s]: 错误域=NSURLErrorDomain 代码=-1005 "网络连接丢失。" UserInfo=0x88b5c30 {NSErrorFailingURLStringKey=https://www.ez-point.com/search, NSErrorFailingURLKey=https://www.ez-point.com/search, NSLocalizedDescription=网络连接丢失。, NSUnderlyingError=0x1bc83790 "网络连接丢失。"}

在 REST 客户端中,我使用参数 Authorization:xxxxxx 和请求有效负载 search_text: UJO526 对 https://www.ez-point.com/search 执行 GET 操作

Rest Client used

【问题讨论】:

    标签: ios objective-c afnetworking


    【解决方案1】:

    试试这个

      AFHTTPClient *client=[[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:@"https://www.ez-point.com/search"]];
    
        [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
        [client registerHTTPOperationClass:[AFJSONRequestOperation class]];
    
        [client setDefaultHeader:@"Authorization" value:@"xxxxxxx"];
        [client setDefaultHeader:@"Content-type" value:@"application/json"];
        [client setDefaultHeader:@"Accept" value:@"application/json"];
    
        NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc]initWithCapacity:1];
        [jsonDic setValue:@"UJO526" forKey:@"search_text"  ];
    
    
        [client getPath:@"" parameters:jsonDic success:^(AFHTTPRequestOperation *operation, id responseObject)
        {
            NSArray *searchResults = (NSArray *) responseObject;
            if ([searchResults count] == 1){
                id result = [searchResults objectAtIndex:0];
                double latitude = [[result valueForKey:@"latitude"] doubleValue];
                double longitude = [[result valueForKey:@"longitude"] doubleValue];
                NSString *ezPoint = [result valueForKey:@"value"];
                NSString *tags = [result valueForKey:@"tags"];
    
                [self setAnnotation:latitude  ForLongitude:longitude withEZPoint:ezPoint WithTags:tags];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
        }];
    

    【讨论】:

    • 使用您的代码,我收到此错误:2013-11-04 15:39:52.517 EZ-POINT[4489:c07] E restkit.network:RKObjectRequestOperation.m:209 GET '@ 987654321@'
    • 这将返回 404 错误,因为在 URL 末尾添加了额外的文本
    • 确定这是一个get方法吗?
    • 添加截图到qn你在restclient中的表现
    • 我无法添加屏幕截图,但我添加了一小段文字,描述了我在问题结束时所做的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2012-05-17
    • 2015-12-27
    相关资源
    最近更新 更多