Play cool
Play cool

Reputation: 158

AFHTTPRequestOperation fail

I am developing an app which perform following task.

  1. Fetch gallery images and display in grid view.
  2. after selecting images, they will be uploaded to server

I use "POST" method for upload. It is working fine into iPhone simulator. But into iPhone 5 device, it will create following error after some progress of uploading.

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x1f0ba650 {NSErrorFailingURLStringKey=http://192.168.1.25/webservices/uploadphoto.php/?user_id=22, NSErrorFailingURLKey=http://192.168.1.25/webservices/uploadphoto.php/?user_id=22, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1f0a3b60 "The request timed out."}

Here, my code for uploading.

    -(void)uploadImageWithStatus:(NSData *)data filenumber:(NSInteger)filenumber{ NSUserDefaults *defaultUser = [NSUserDefaults standardUserDefaults];
        NSString *userId = [defaultUser stringForKey:@"UserId"];

        AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",UploadURL,userId]]];
        client.operationQueue.maxConcurrentOperationCount =1;

        //You can add POST parameteres here

        NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:nil];

        NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:nil parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {

            //This is the image
            [formData appendPartWithFileData: data name:@"f" fileName:[NSString stringWithFormat:@"%d_image.jpeg",rand()] mimeType:@"image/jpeg"];

        }];


       NSLog(@"Time out : %f",[request timeoutInterval]);
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {


            //        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

}];
        //Setup Completeion block to return successful or failure
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *response = [operation responseString];
            NSLog(@"response: [%@]",response);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error: %@", [operation error]);
            if(error.code == -1001){
            UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                              message:@"The request timed out." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [myAlert show];
            }
            //Code to Run if Failed

        }];

        [operation start];

I will be very thankful, if anyone can help me. Thanks in advanced.

Upvotes: 1

Views: 1157

Answers (1)

lxt
lxt

Reputation: 31294

The error you're receiving is a time-out error: the server you're trying to contact is failing to respond in a timely fashion.

I notice that you're trying to connect to 192.168.1.25, a local IP address. Is it possible that you are running a server locally on your development machine (so the iPhone simulator works fine), but can't access this on your iPhone device as it's on a different network?

The best way to test this is to go into Mobile Safari and load up 192.168.1.25. If it doesn't work, you'll know the problem isn't with your code, but is with how your network is set up.

Upvotes: 2

Related Questions