Noor
Noor

Reputation: 20150

Getting issue of network connection was lost with AFNetworking

I am trying to send a request with AFNetworking, but I'm getting a problem of "The network connection was lost" although the request is good. I'm trying to send the request with a client, and I'm getting the proper response.

The details of the request are: URL: https://www.ez-point.com/search Method: GET Authorization header: xxxxxxxxxxxxxxx Request Payload: "search_text":"value"

Here is the code I'm using:

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];

The error I'm getting is:

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]: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x88b5c30 {NSErrorFailingURLStringKey=https://www.ez-point.com/search, NSErrorFailingURLKey=https://www.ez-point.com/search, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x1bc83790 "The network connection was lost."}

in the REST Client, I'm doing a GET on https://www.ez-point.com/search with the parameters, Authorization:xxxxxx and with Request Payload search_text: UJO526

Rest Client used

Upvotes: 0

Views: 1963

Answers (1)

Lithu T.V
Lithu T.V

Reputation: 20021

Try this

  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) {

    }];

Upvotes: 1

Related Questions