我正在开发一个应用程序,执行以下任务。
我使用"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];如果有人能帮我,我会非常感激的。谢谢你的进阶。
发布于 2013-01-02 09:55:18
您收到的错误是一个超时错误:您试图联系的服务器未能及时响应。
我注意到您正在尝试连接到192.168.1.25,一个本地IP地址。您是否有可能在您的开发机器上本地运行服务器(因此iPhone模拟器可以正常工作),但无法在您的iPhone设备上访问它,因为它位于不同的网络上?
测试这一点的最好方法是进入Mobile并加载192.168.1.25。如果它不起作用,您就会知道问题不在您的代码中,而是在于您的网络是如何设置的。
https://stackoverflow.com/questions/14119820
复制相似问题