首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AFHTTPRequestOperation失败

AFHTTPRequestOperation失败
EN

Stack Overflow用户
提问于 2013-01-02 09:46:02
回答 1查看 1.2K关注 0票数 1

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

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

我使用"POST“方法上传。它可以很好地工作在iPhone模拟器中。但是在iPhone 5设备上,它会在上传过程中产生以下错误。

代码语言:javascript
复制
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."}

这是我上传的代码。

代码语言:javascript
复制
    -(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];

如果有人能帮我,我会非常感激的。谢谢你的进阶。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-02 09:55:18

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

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14119820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档