1

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

  1. 获取画廊图像并在网格视图中显示。
  2. 选择图像后,它们将被上传到服务器

我使用“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];

如果有人可以帮助我,我将非常感激。提前致谢。

4

1 回答 1

2

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

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

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

于 2013-01-02T09:55:18.597 回答