AFHTTPRequestOperation失败

时间:2013-01-02 09:46:02

标签: objective-c ios6

我正在开发一个执行以下任务的应用程序。

  1. 获取图库图像并以网格视图显示。
  2. 选择图像后,它们将上传到服务器
  3. 我使用“POST”方法进行上传。 它在iPhone模拟器中运行良好。 但是进入iPhone 5设备后,在上传一些进度后会产生以下错误。

    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."}
    

    这是我的上传代码。

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

    如果有人能帮助我,我将非常感激。谢谢你提前。

1 个答案:

答案 0 :(得分:2)

您收到的错误是超时错误:您尝试联系的服务器无法及时响应。

我注意到您正在尝试连接到本地IP地址192.168.1.25。您是否有可能在开发计算机上本地运行服务器(因此iPhone模拟器工作正常),但无法在iPhone设备上访问它,因为它位于不同的网络上?

测试此功能的最佳方法是进入Mobile Safari并加载192.168.1.25。如果它不起作用,您将知道问题不在于您的代码,而在于您的网络设置方式。

相关问题