nsurlconnection - Domain=NSURLErrorDomain Code=-1001 "The operation couldn’ 待完成。 (NSURLErrorDomain 错误-1001。)”

标签 nsurlconnection afnetworking nsurlrequest nsurlsession xcode6-beta6

我的程序是为从摄像头上传图片而编写的,示例代码如下:



    #define WEBSERVICE_URL @"http://192.168.0.104/upload.php"
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

        [picker dismissViewControllerAnimated:YES completion:^{

            UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

            NSData *imageData = UIImagePNGRepresentation(selectedImage);

            NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:WEBSERVICE_URL parameters:nil constructingBodyWithBlock:^(id formData) {
                [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test" mimeType:@"image/png"];
            } error:nil];

            AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

            NSProgress *progress = nil;

            NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

                [progress removeObserver:self forKeyPath:@"fractionCompleted"];
                NSLog(error.debugDescription);


                if (error) {
                    [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
                } else {
                    [self.view updateWithMessage:@"Great success!"];

                }
            }];

            [progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];

            [uploadTask resume];

            self.imageUploadProgress = [[TNSexyImageUploadProgress alloc] init];
            self.imageUploadProgress.radius = 100;
            self.imageUploadProgress.progressBorderThickness = -10;
            self.imageUploadProgress.trackColor = [UIColor blackColor];
            self.imageUploadProgress.progressColor = [UIColor whiteColor];
            self.imageUploadProgress.imageToUpload = selectedImage;
            [self.imageUploadProgress show];

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUploadCompleted:) name:IMAGE_UPLOAD_COMPLETED object:self.imageUploadProgress];

        }];
    }

错误是:

TNSexyImageUploadProgressDemo[5275:113032] Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x7f92c2d907d0 {NSErrorFailingURLStringKey="<code>http://192.168.0.104/upload.php</code>", NSUnderlyingError=0x7f92c2dcd5c0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)", NSErrorFailingURLKey="h<code>ttp://192.168.0.104/upload.php</code>"}

whatever on simulator or iPhone,the same error,i use safari to access url,anything is correct,i use wifi to access network. ping 192.168.0.104 is ok.

server's program is write in php,code blelow:



     100000000) {
        $result_json['error'] = 'Exceeded filesize';
    }

    $finfo = new finfo(FILEINFO_MIME_TYPE);

    if (false === $ext = array_search(
        $finfo->file($_FILES['upfile']['tmp_name']),
            array(          
                'png' => 'image/png'           
            ),
            true
        )) {
            $result_json['error'] = 'Invalid file format';
    }

    if (!move_uploaded_file(
        $_FILES['upfile']['tmp_name'],
        sprintf('./uploads/%s.%s',
            sha1_file($_FILES['upfile']['tmp_name']),
            $ext
        )
        )) {
            $result_json['error'] = 'Failed to move uploaded file';
    }

    // send the result now
    echo json_encode($result_json);

    /*
    try {

        if (!move_uploaded_file(
            $_FILES['upfile']['tmp_name'],
            sprintf('./uploads/%s.%s',
                sha1_file($_FILES['upfile']['tmp_name']),
                $ext
            )
            )) {
                //throw new RuntimeException('Failed to move uploaded file.');
        }

        //echo json_encode(array('succes'=>true));
    } catch (RuntimeException $e) {

        //echo $e->getMessage();

    }
    */
    ?>

最佳答案

自从我开始使用 Xcode 6 以来,我遇到了同样的问题。更新 AFNetworking 库解决了这个问题。一切又恢复正常了。

https://github.com/AFNetworking/AFNetworking

如果您使用的是 Xcode 6,请尝试一下。

关于nsurlconnection - Domain=NSURLErrorDomain Code=-1001 "The operation couldn’ 待完成。 (NSURLErrorDomain 错误-1001。)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708289/

相关文章:

ios - NSUrlConnection sendAsynchronousRequest 和自签名证书

ios - 为什么使用 NSURLConnection sendAsynchronousRequest 建立与数据库的连接会延迟?

ios - 在 swift 3.2 中使用 json 和 header 请求 AFNetworking

ios - 如何在 Swift 中设置 UIWebView 的 "User-Agent" header

ios - 如何使用非英文字母(ø、æ 等)将 HTTP header 添加到 NSURLMutableRequest。让它开始工作

iphone - NSURLConnection 委托(delegate)方法未被调用

ios - 中断执行 AFHTTPRequestOperation

ios - 在 AFJSONRequestOperation 中接受无效的 JSON?

带有二进制正文的 iOS 快速发布请求

ios - NSURLConnection:我正在尝试接收 html 页面,但总是收到错误 403