- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
首先,所有的问题都很简单。如果你只是想看看它们是什么,请跳到这篇文章的底部,你会看到它们以粗体显示。要了解更多细节,你可以阅读这篇文章的其余部分...
我只是想解决我的 NSURLConnection 问题,以便它能顺利工作,而且我也能正确理解这一点。互联网上非常缺乏异步连接的示例/教程,或者我找不到任何可以解释任何深度级别的示例/教程,除了启动和运行连接之外,这在处理后看起来非常简单。希望这个问题可以填补其他用户的空白。
因此,在我的 .h 文件中,我导入了基础 header 并声明了接收到或缺少接收到的数据(错误等)所需的方法。
.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h> //add foundations
//.. other headers can be imported here
@interface MyViewController: UITableViewController {
//Im not setting any delegates to access the methods because Is all happening in the same
//place so I just use the key word 'self' when accessing the methods declared below
//I'm not sure if this is the best thing to do but I wasn't able to get my head around declaring the delegate or how it would help me with the way I have set up my request etc.
}
- (IBAction)setRequestString:(NSString *)string; //this method sets the request and connection methods
//these methods receive the response from my async nsurlconnection
- (void)receivedData:(NSData *)data;
- (void)emptyReply;
- (void)timedOut;
- (void)downloadError:(NSError *)error;
这就是我的头文件.. 非常简单,不需要太多解释。
.m
//call setRequestString from some other method attached to a button click or something
[self setRequestString:@"rss.xml"];
//..
- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http:www.comicbookresources/feeds/"]; // address not real jsut example
//append the string coming in to the end of the databaseURL
[databaseURL appendString:string];
//prepare NSURL with newly created string
NSURL *url = [NSURL URLWithString:databaseURL];
//AsynchronousRequest to grab the data
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil){
[self receivedData:data];
}else if ([data length] == 0 && error == nil){
[self emptyReply];
}else if (error != nil && error.code == NSURLErrorTimedOut){ //used this NSURLErrorTimedOut from foundation error responses
[self timedOut];
}else if (error != nil){
[self downloadError:error];
}
}];
}
现在设置在 .h 文件中初始化并在上面的 if 语句中调用的方法
- (void)receivedData:(NSData *)data
{
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr); //logs recived data
//now you can see what data is coming in on your log
//do what you want with your data here (i.e. start parsing methods
}
- (void)emptyReply
{
//not sure what to do here yet?
}
- (void)timedOut
{
//also not sure what to do here yet?
}
- (void)downloadError:(NSError *)error
{
NSLog(@"%@", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A connection failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
太棒了,我在那里所做的几乎是基础知识..现在我的问题如下。
问题一:我在哪里调用 NSURLConnection
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
这里发生了什么 ^ 是为了在不同的线程或其他东西上执行整个 block (包括 if 语句)?因为它看起来很像中央调度格式,但略有不同。
问题二:我应该在 emptyReply 和 timedOut 方法中做什么?
问题三:我将如何将缓存合并到其中?我想缓存从不同请求中得到的响应。即使用我的 setRequestString 你会看到有一个字符串输入参数,所以我可以用相同的方法请求不同的 rss 提要..我需要弄清楚如何将这些响应缓存到单独的缓存中..但我不确定从哪里开始
最后如果您已经做到这一点,非常感谢您阅读我的问题。希望通过您的回复,我们可以在这里得到一个非常好的解决方案。其他人可以自己使用,并挑选他们需要的适用于他们自己的解决方案的位和 peices。
无论如何,非常感谢您的阅读,我期待您的回复。即使它们只是对您认为可能对我有帮助的教程或示例的引用。任何东西都很好,我只是想完全了解发生了什么以及发生了什么一个很好的解决方案。
最佳答案
尝试缓存
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL];
关于iphone - nsurlconnection异步请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9286849/
我正在寻找有关使用 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 的小错误。我已经追踪到一个意外的连接保持事件,这似乎使网络服务器感到困惑(可能是服务器端的错误)。解决方法是在某个时刻强制关闭所有未完成
我是一名优秀的程序员,十分优秀!