2013-11-04 83 views
0

我試圖發送一個請求與AFNetworking,但我遇到了「網絡連接丟失」的問題,雖然請求是好的。我正在嘗試向客戶端發送請求,並且我得到了正確的響應。獲得網絡連接的問題與AFNetworking丟失

請求的細節是: 網址:https://www.ez-point.com/search 方法:GET Authorization頭:XXXXXXXXXXXXXXX 請求負載: 「SEARCH_TEXT」: 「值」

這裏是我使用的代碼:

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月4日15:01:05.143 EZ-POINT [4074:C07]ê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客戶

,我正在做一個GET上https://www.ez-point.com/search與參數,授權:XXXXXX,並請求負載SEARCH_TEXT:UJO526

Rest Client used

回答

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

    }]; 
+0

隨着你的代碼,我得到這個錯誤:2013-11-04 15:39:52.517 EZ-POINT [4489:c07] E restkit.network:RKObjectRequestOperation.m:209 GET'https://www.ez-point。 com/search /?search_text = UJO526' – Noor

+0

這是返回404錯誤,因爲在URL末尾添加了額外的文本 – Noor

+0

確定這是一個get方法? –