0

I use the below code to upload image to server.

NSString *url = [NSString stringWithFormat:@"http://192.250.1.52:xxx/api/user/profileUploadUser?userid=27&emailid=tom@gnts.in"];

 NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc]init];

[jsonDict setObject:ImageToUpload forKey:@"file"];

////////ImageToUpload = /var/mobile/Containers/Data/Application/89112E0B-C1D6-4408-8586-6C5B4A431713/Documents/61.png

    [manager POST:url parameters:jsonDict
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     { //////success code }


         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Image upload" message:@"failure" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
             [alert show];
             NSLog(@"error message  %@",error);
             // handle failure
         }];

It always enter into the failure block and shows the error message.

error message : Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=http://192.250.1.52:xxxx/api/user/profileUploadUser?userid=27&emailid=tom@gnts.in, _kCFStreamErrorCodeKey=-21022}}}

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
Lenin
  • 675
  • 6
  • 17

2 Answers2

0

Check your internet speed might be slow,

[manager.requestSerializer setTimeoutInterval:30];//Set request timeout interval time & interact with backend developer for same.

One more thing you can manage one retry counter in singleton & give second request if fails & if again fails then show toast message internet speed is slow.

Mukesh Lokare
  • 2,159
  • 26
  • 38
  • It's upto you how many time you have to give request & don't forget to reset `retryCounter` to 0 again in both condition(I mean reset retryCounter to zero once you in success block or show error toast message) – Mukesh Lokare Feb 15 '17 at 09:07
  • I put the timeout interval code here, but it's not solve my issue. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.requestSerializer.timeoutInterval=30; – Lenin Feb 15 '17 at 09:17
  • Set time interval to 60 & check & even not worked then you can manage one `retryCounter' singleton give request twice & even if fails you have to handle toast message that **Try Again, Request Time Out** its because of internet speed issue. I don't think issue is different rather than internet speed & please for request interval time you can discuss one time with backend developer. if you got another solution let me know – Mukesh Lokare Feb 15 '17 at 09:34
  • Ohh it seems you are uploading file only. Visit http://stackoverflow.com/a/15413152/4294543 – Mukesh Lokare Feb 15 '17 at 09:48
  • Hi Mukesh, I use the code from above url, But it shows error "unrecognizes selector" NSURL *localFileToUpload = [NSURL fileURLWithPath:ImageToUpload]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager POST:url parameters:nil constructingBodyWithBlock:^(id formData) { [formData appendPartWithFileURL:localFileToUpload name:@"files" fileName:@"image.png" mimeType:@"image/jpeg"]; – Lenin Feb 15 '17 at 10:13
  • make sure you are correctly retain/release objects make zombie state enable from edit scheme go to diagonosis & allow all obj-c exceptions .... this will help you to track your issue. – Mukesh Lokare Feb 15 '17 at 10:22
0

You are trying to a POST operation whereas what you want is a file upload.
Try out AFNetworking multipart file upload.

ystack
  • 1,785
  • 12
  • 23