- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试迁移到 NSURLSession,这样我就可以将多个图像发送到我的服务器并在收到所需响应时停止。但是我的完成 block 永远不会被调用。如果您能告诉我我是否正确地进行了迁移,我们将不胜感激。
这是我的旧代码,运行良好-
-(void) sendImgWithText:(UIImage*)img
{
NSURL *requestURL = [NSURL URLWithString:@"Some URL"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"*****";
NSString *lineEnd = @"\r\n";
NSString *twoHyphens = @"--";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
// UIImage *imgColor = [UIImage imageNamed:@"9.jpg"];
UIImage * imageToPost = [[UIImage alloc] init];
UIImageWriteToSavedPhotosAlbum(imageToPost, nil, nil, nil);
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"%@%@%@", twoHyphens,boundary, lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"%@%@%@%@", twoHyphens,boundary, twoHyphens,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSLog(@"%@",requestURL);
NSLog(@"%f,%f",imageToPost.size.height,imageToPost.size.height);
// set URL
[request setURL:requestURL];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
这是版本(我从其他堆栈溢出文章中得到大部分)
- (void) serverRequestWithImage:(UIImage *)img completion:(void (^)(id responseObject, NSError *error))completion
{
NSURL *requestURL = [NSURL URLWithString:@"Some URL"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"*****";
NSString *lineEnd = @"\r\n";
NSString *twoHyphens = @"--";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
UIImage * imageToPost = [[UIImage alloc] init];
imageToPost = img;
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"%@%@%@", twoHyphens,boundary, lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.jpg\"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"%@",lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"%@%@%@%@", twoHyphens,boundary, twoHyphens,lineEnd] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSLog(@"%@",requestURL);
NSLog(@"%f,%f",imageToPost.size.height,imageToPost.size.height);
// set URL
[request setURL:requestURL];
NSURLSessionTask *task =
[session
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
// report any network-related errors
NSLog(@"Got Response 1");
if (!data) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}
return;
}
// report any errors parsing the JSON
NSError *parseError = nil;
_responseData = [NSMutableData dataWithData:data];
if (_responseData) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, parseError);
});
}
return;
}
// if everything is ok, then just return the JSON object
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(_returnedData, nil);
});
}
}];
[task resume];
}
这是我在其他类(class)的调用方法
- (IBAction)GetData:(id)sender
{
[_imageView setImage:[UIImage imageNamed:@"9.jpg"]];
msg = [[Message alloc] init];
[msg initData];
msg.delegate=self;
[msg serverRequestWithImage:_imageView.image completion:^(id responseObject, NSError *error)
{
if (responseObject) {
// do what you want with the response object here
NSLog(@"Got Data");
} else {
NSLog(@"%s: serverRequest error: %@", __FUNCTION__, error);
}
}];
}
最佳答案
项目:
我没有看到 NSURLSession
的初始化。这是你应该在你的问题中展示的东西。您还可以检查 session
是否为非零的“发送图像”方法。
看起来您是从一个文件 (@"9.jpg"
) 开始的。如果是这样,-[NSURLSession uploadTaskWithRequest:fromFile:completionHandler:]
可能会为您省去很多麻烦。
未经请求的建议,包含所有内容::)
在我看来,-serverRequestWithImage:completion:
是该方法的糟糕名称。它暗示它返回一个请求对象,并没有告诉你它实际上发送了请求。一些活跃的东西,比如 -uploadImage:completionHandler:
可能会更好。
按照惯例,所有方法名称都应以小写字母开头,即 -getData:
。
关于ios - 从 NSURLConnection 迁移到 NSURLSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148209/
我正在寻找有关使用 NSURLConnection 异步请求的好教程。我一直在 stackoverflow 和谷歌中四处寻找,但找不到。这可能是无数类似此处问题的重复。但请指导我正确的教程,我以前使用
我在植入简单的 NSURLConnection 时遇到了这个奇怪的问题... 调用了 didReceiveData 方法,我很乐意尝试附加接收数据,但是... nada! 肯定有一些数据(如长度所示,
我是 iOS 互联网连接的新手。 我正在尝试从特殊站点获取数据。 以下代码适用于所有站点以查看 url 中的数据。 但是,如果我将其更改为特殊站点以获取他们的数据,它将返回 NULL! 我认为该站点如
在 iOS 9 中,我将 NSURL 与 NSURLConnection 一起使用,对于一个特定的 URL,它在 - (void)connection:(NSURLConnection *)conne
多个 NSURLConnections 正在启动(在单个 UIViewController 中)以收集不同类型的数据。当他们返回(-connectionDidFinishLoading)时,我想根据到
谁能帮助我理解这些问题:使用 Alamofire 优于 NSURLSession/NSURLConnection 有什么优势? NSURLSession 和 NSURLConnection 有什么区别
是否可以取消 NSURLConnection开始于 sendAsynchronousRequest:queue:completionHandler: ? 为什么不sendAsynchronousReq
你好, 我正在试验一个非常恶性的崩溃,它在设备日志中以下列方式显示: Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_I
这是一个开放性问题,旨在了解什么是我认为可能很常见的问题的最佳实践或最常见的解决方案。 假设我有一个要下载的 URL 列表;该列表本身托管在服务器上,因此我启动了一个 NSURLConnection
我基本上将RestKit与RKClient一起使用,并执行get请求。我的系统使用用户名/密码/帐户来认证用户。它只是用于用户名/密码的httpbasic,而帐户只是一个子域。 我的问题是,如果执行以
我尝试使用 [sendSynchronousRequest: returnedResponse: error:] 代码连接服务器,但证书未经验证,导致出现错误。我已经读过 How to use NSU
我正在使用 NSURLConnection 通过 GET 请求与服务器连接。要登录,我将发送带有表单 url 的 GET 请求 http://:@my.serverurl.com/user/login
我正在将图像上传到我的服务器,当我上传图像时,我试图向用户显示上传进度条。但在 NSURLConnection 委托(delegate)方法 连接中: didSendBodyData:totalByt
这是另一个简单的问题,我在 Google 上搜索了一个小时后却找不到答案... 用户正在文本字段中输入内容,当该字段失去焦点时,我启动 NSURLConnection.... NSString *us
我正在尝试验证连接是否成功,但得到的结果不稳定。当我尝试使用虚假网址执行同步请求时: NSData *responseData = [NSURLConnection sendSynchronousRe
我正在将图像加载到 iPhone 上的图库中,但遇到了问题。显然,当尝试从互联网下载图像时,脚本中的某些内容对文件名中的空格不满意。 这是连接线。 NSURLConnection *conn = [[
我有一个 NSURLConnection,每次调用 -(void)viewWillAppear:animated 时都会调用它(仅用于测试) 我就是这样做的 receivedData = [[NSMu
在 NSURLConnection 上调用实例方法 cancel 通常根本不会取消连接。 我正在 NSOperation 中执行异步 NSURLConnection。如果取消操作,则通过调用 NSUR
当我允许加载完成时,我有一个 NSURLConnection 工作正常。但是如果用户点击后退按钮,意味着 webview 将消失,我想取消正在进行的 NSURLConnection。但是,如果在调用
我的客户端应用程序中有一个使用 NSURLConnection 的小错误。我已经追踪到一个意外的连接保持事件,这似乎使网络服务器感到困惑(可能是服务器端的错误)。解决方法是在某个时刻强制关闭所有未完成
我是一名优秀的程序员,十分优秀!