我正在开发一个执行以下任务的应用程序。
- 获取画廊图像并在网格视图中显示。
- 选择图像后,它们将被上传到服务器
我使用“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];
如果有人可以帮助我,我将非常感激。提前致谢。