Tag: afnetworking

如何在AFNetworking 2.0中设置请求超时和caching策略?

我遵循给定的示例代码 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; 要更改超时和caching策略,我“侵入”了库并创build – (AFHTTPRequestOperation *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters timeoutInterval:(NSTimeInterval)timeoutInterval cachePolicy:(NSURLRequestCachePolicy)cachePolicy success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] […]

如何在JSONRequestOperation上将自定义标题添加到AFNetworking

嗨,我有以下代码访问一个URL: NSString * stringURL = [NSString stringWithFormat:@"%@/%@/someAPI", kSERVICE_URL, kSERVICE_VERSION]; NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringURL]]; AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { completionHandler(JSON, nil); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { completionHandler(nil, error); }]; 但是我希望将用户标记作为HEADER上的parameter passing,如X-USER-TOKEN 。 不能在AFNetworking documentationfind它,我应该改变操作types?

UICollectionView – 下载图像并有效地重新加载数据,比如Instagram

我注意到像Intagram这样的应用程序使用UICollectionViews来显示照片的提要。 我也注意到这些照片的单元格在实际的照片被完全下载之前以某种方式放置在屏幕上。 然后,当下载完成时,该照片很好地显示在正确的单元格中。 我想复制这个function,但我不知道如何去做。 我目前正在下载一个大的JSON对象,我转换成一个NSDictionaries数组。 每个NSDictionary包含有关每张照片的信息,并且在这些信息当中显示一个URL。 在这个URL,我可以find我需要下载并显示在我的UICollectionViewCells相应的图像 至于现在,我迭代这个列表,并开始下载我看到的每个URL。 当下载完成后,我使用[self.collectionView reloadData]重新加载collectionview。 但是,正如你所想象的,如果我有30个单元都需要一个图像,那么会有很多reloadData调用。 我正在使用AFNetworking来处理下载,这里是我根据我之前提到的URL调用的方法: -(void) downloadFeedImages:(NSString *) photoURL imageDescs:(NSDictionary*)imageDescs photoId:(NSString *)photoID{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *directory = [paths objectAtIndex:0]; NSString* foofile = [directory stringByAppendingPathComponent:photoID]; if([[NSFileManager defaultManager] fileExistsAtPath:foofile]){ // IF IMAGE IS CACHED [self.collectionView reloadData]; return; } NSLog(@"photoURL: %@", photoURL); NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL […]

AFNetworking – 如何发出POST请求

编辑07/14 正如Bill Burgess在回答他的评论时提到的,这个问题与AFNetworking version 1.3有关。 这里的新手可能已经过时了。 我对iPhone开发非常陌生,我正在使用AFNetworking作为我的服务库。 我查询的API是一个RESTful的,我需要做POST请求。 要做到这一点,我试着用下面的代码: NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil]; NSURL *url = [NSURL URLWithString:@"http://localhost:8080/login"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"Pass Response = %@", JSON); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { […]

POST请求与JSON正文AFNetworking 2.0

无论如何,使用AFNetworking〜> 2.0发送带有JSON主体的POST请求? 我曾尝试使用: manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager POST:<url> parameters: @{@"data":@"value"} success: <block> failure: <block>' 但它不起作用。 任何帮助是极大的赞赏。 谢谢

AFJSONRequestOperation数组填充,但不能NSLog成功块外的内容

以下代码摘自本教程 我之前使用过这个片段,但之前从未注意过这个问题。 数组内容的NSLog将在委托方法中打印,但不在成功块外部的viewDidLoad中打印。 我需要一种方法将JSON数据保存到一个数组中,以便在代码中的其他地方使用。 我还应该补充说,我没有使用UITableView来显示我的数据。 我错过了什么,或者我该如何做到这一点? 这不会打印JSON内容,因为它会填充数组: #import "AFNetworking.h" … – (void)viewDidLoad { … self.movies = [[NSArray alloc] init]; NSURL *url = [[NSURL alloc] initWithString:@"http://itunes.apple.com/search?term=harry&country=us&entity=movie"]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { self.movies = [JSON objectForKey:@"results"]; [self.activityIndicatorView stopAnimating]; [self.tableView setHidden:NO]; [self.tableView reloadData]; } failure:^(NSURLRequest […]

库找不到-lPods-AFNetworking

使用AFNetworking时出现以下错误: ''库找不到-lPods-AFNetworking'' “链接器命令失败,退出代码1(使用-v查看调用)”。 我检查了所有缺less的框架,它们都是存在的。此外,这个项目适用于其他人(我们从github中获取),而且我是唯一不能工作的人(它是一个联合项目) 我们都使用Xcode 6.2。 我不明白什么可能是错的或什么是失踪。 我尝试使用命令行,sourceTree,甚至从Xcode git源代码控制拉。 我也尝试过不同版本的Xcode。 但所有其他teamates使用Xcode 6.2,我现在正在使用。 过去工作之前,它突然停止工作。 任何想法都欢迎,谢谢! 以下是详细的错误: ld:warning:找不到选项'-L / Users / ramapriyasridharan / Documents / Rama-3:06:2015-ios / Mapbox'ld:warning:找不到选项'-L / Users / ramapriyasridharan / Documents / Rama-3:06:2015-ios / Pods / build / Debug-iphoneos'ld:library找不到-lPods-AFNetworking clang:error:linker命令失败,退出代码1(使用-v查看调用) 编辑: 打开工作区文件后,我没有得到马赫O链接器错误,但我得到以下错误: Command /Volumes/Xcode 1/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/momc failed with exit code 1 我问我的队友说谁还能正常工作,所以这只是我的电脑问题!

在AFNetworking 2.0caching

所以这是交易。 我最近开始使用AFNetworking在开始时使用下面的代码下载几个文件: NSMutableURLRequest* rq = [api requestWithMethod:@"GET" path:@"YOUR/URL/TO/FILE" parameters:nil]; AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:rq] autorelease]; NSString* path=[@"/PATH/TO/APP" stringByAppendingPathComponent: imageNameToDisk]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"SUCCCESSFULL IMG RETRIEVE to %@!",path) } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Deal with failure }]; 随着我的path实际上插入到pathvariables(对不起,现在不是正确的计算机上复制意大利面的文本,但它是完全一样的上述不同的path的东西) 而且一切都很好! 我正在成功下载文件和一切。 我目前的问题是,我试图caching工作,但我有很多困难。 基本上,我不确定在AFNetworking 2.0中,我真的需要做客户端。 我是否还需要设置NSURlCache? 我是否需要对请求操作设置cachingtypes标头? […]

AFNetworking 2.0在代码块400中从代码400获取JSON

我正在使用AFHTTPRequestOperationManager POST请求。 现在我故意input不正确的信息来处理400错误代码。 现在,Web服务实际上返回一个JSON ,并向用户解释他们做错了什么。 我非常希望得到这个JSON在UIAlertView显示消息。 但是,以下的失败块: [operationManager POST:ServerURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: Status Code: %d", operation.response.statusCode); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failed: Status Code: %d", operation.response.statusCode); }]; 不会像成功块中的那样传递响应对象。 那么有没有人知道我可以访问由Web服务返回的400错误的JSON? NSError *error只是给我Request failed: bad request (400)而不是返回的JSON。 任何帮助,将不胜感激, 麦克风

如何在iOS上使用大file upload?

我的应用程序需要从用户手机上传video文件,然后在服务器上处理。 问题是文件的大小可能会增加到200 MB,并且用户不会保持应用程序打开以等待file upload。 由于苹果不允许应用程序在超过有限的时间在后台运行。 我怎样才能确保我的file upload。 我正在使用networking连接来设置ios 7库提供的上传任务。 请如果任何人可以指出我在正确的方向或有任何解决scheme,将不胜感激。 我已经长时间地在这个问题上ban咽了。 谢谢。 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; [manager setTaskDidSendBodyDataBlock:^(NSURLSession *session,NSURLSessionTask *task ,int64_t bytesSent, int64_t totalBytesSent,int64_t totalBytesExpectedToSend){ CGFloat progress = ((CGFloat)totalBytesSent / (CGFloat)sensize); NSLog(@"Uploading files %lld — > %lld",totalBytesSent,totalBytesExpectedToSend); [self.delegate showingProgress:progress forIndex:ind]; }]; dataTask = [manager uploadTaskWithStreamedRequest:request progress:nil completionHandler:^(NSURLResponse *response, […]