kanyun-inc / ytknetwork Goto Github PK
View Code? Open in Web Editor NEWYTKNetwork is a high level request util based on AFNetworking.
License: MIT License
YTKNetwork is a high level request util based on AFNetworking.
License: MIT License
你好,我现在使用了缓存选项,那我如何能把这个服务器取出的数据处理后再存入缓存,下次访问这个api时直接能从缓存中取出修改后的缓存数据呢。
谢谢。
如题
从YTKNetwork库来看的话,目前的AFNetworking并不是由cocospod管理起来的,那么问题来了,如果AFNetworking版本更新了,YTKNetworking如何更新第三方库?
看到 block返回的对象是YTKBaseRequest, 如果我想针对某些请求,返回Mantle 序列化后的对象. 我应该重写什么方法. 3Q.
还是感谢开源这么好的东西.
YTKNetwork最低适配的iOS系统版本是由AFNetworking决定的么?
我想在http头部加一个数据,应该如何做?
如何使用YTKNetwork传递token?
当stop request的时候 在stop消息中以及其中的clearRequest 重复调用了[self ...stopCallBack]的响应stop的回调方法
目前场景是在搜索情况下,再不同类型进行快速网络请求搜索,切换的时候我们会把当前的请求终止并启动新线程,如果非常快速点击的情况下,可能会因为 stop 请求报 double free 的崩溃错误。
如果把并发线程数改为 1 就没有这个问题。
代码如下,
- (void)viewDidLoad
{
[super viewDidLoad];
SportsApi *api = [[SportsApi alloc] init];
YTKChainRequest *chainRequest = [[YTKChainRequest alloc] init];
[chainRequest addRequest:api callback:^(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest) {
SportsApi *result = (SportsApi *)baseRequest;
self.sports = result.responseJSONObject;
FavoriteSportsApi *nextApi = [[FavoriteSportsApi alloc] initWithMethod:@"GET"];
[chainRequest addRequest:nextApi callback:^(YTKChainRequest *chainRequest, YTKBaseRequest *baseRequest) {
FavoriteSportsApi *result = (FavoriteSportsApi *)baseRequest;
self.selectedSports = result.responseJSONObject;
self.tempSelectedSports = self.selectedSports.mutableCopy;
}];
}];
chainRequest.delegate = self;
}
- (void)chainRequestFinished:(YTKChainRequest *)chainRequest {
[self.tableView reloadData];
}
- (void)chainRequestFailed:(YTKChainRequest *)chainRequest failedBaseRequest:(YTKBaseRequest*)request {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"异常提示" message:@"网络异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
连续快速调用如下方法会导致程序崩溃。调用次数不确定,有时候四五次,有时候更多的时候崩溃。
(void)getAutoComplateWithKeyWord:(NSString *)keyWord {
if (self.autoComplateRequest.isExecuting) {
[self.autoComplateRequest stop];
}
self.autoComplateRequest = [[QYAutoComplateRequest alloc] initWithKeyWord:keyWord];
self.autoComplateRequest.isShowToast = NO;
[self.autoComplateRequest startWithSuccess:^(id results, QYRequest *request) {
if (request != self.autoComplateRequest) {
return;
}
if (!self.isSearching) {
if ([results isKindOfClass:[NSDictionary class]]) {
self.searchKeyWords = results[@"entry"];
}
}
} failure:^(QYRequestType type, QYRequest *request) {
}];
}
比如我的接口返回都是统一的 code,msg,data . 当 code不为1000时候, 返回错误信息.
我如何在基类判断responseJSONObject, 当返回的 json中code不为1000. 不走 sunccess的 block, 而走 false的 block.并且带回错误的 msg? 是写一个基类, 继承YTKRequest?
3Q
查看了YTKNetwork支持的http动词,发现没有PATCH,是什么原因不支持PATCH呢?
typedef NS_ENUM(NSInteger , YTKRequestMethod) {
YTKRequestMethodGet = 0,
YTKRequestMethodPost,
YTKRequestMethodHead,
YTKRequestMethodPut,
YTKRequestMethodDelete,
};
目前看好像连接超时是一分多钟后响应failure
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
不知道是否可以有设置超时时间, 比如30秒.AF 里是有此设置
在YTKNetworkConfig
中未看到相关属性设置
我看章程, YTKNetwork最近修复了bug,有少许改动, 这些在0.1.0里是没体现的
hi
非常感谢你们分享代码
我在我的项目中尝试使用你们的库
现在遇到一个问题。
https://124.202.153.103:3182/[email protected]
这个网址 我按照你们的教程来做, 就是请求失败。。
我用其他的都不会这样。
不知道是不是网址的问题??
期待你的回复。
#import "YTKRequest.h"
@interface GetImageApi : YTKRequest
@end
#import "GetImageApi.h"
@implementation GetImageApi
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
- (NSString *)requestUrl {
return @"?mod=stand&op=forumtj";
}
- (YTKRequestMethod)requestMethod {
return YTKRequestMethodPost;
}
@end
/////////////////////////////////
#import "YTKRequest.h"
@interface GetUserInfoApi : YTKRequest
- (id)initWithType:(NSString *)type;
@end
#import "GetUserInfoApi.h"
@implementation GetUserInfoApi {
NSString *_type;
}
- (id)initWithType:(NSString *)type {
self = [super init];
if (self) {
_type = type;
}
return self;
}
- (NSString *)requestUrl {
return @"?mod=home&op=favorite";
}
- (YTKRequestMethod)requestMethod {
return YTKRequestMethodPost;
}
- (id)requestArgument {
return @{ @"token": @"YOeQWfl5shWJSKccidIf9AzdiRPI",
@"type":_type,
@"page":@(1)
};
}
@end
////////////////////////////////////////////////////////////////////
@interface ViewController ()
@end
@implementation ViewController
/// Send batch request
- (void)sendBatchRequest {
GetImageApi *a = [[GetImageApi alloc] init];
GetUserInfoApi *b = [[GetUserInfoApi alloc] initWithType:@"all"];
[a startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
NSLog(@"respond11111 = %@",request.responseJSONObject);
} failure:^(YTKBaseRequest *request) {
}];
[b startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
NSLog(@"respond22222 = %@",request.responseJSONObject);
} failure:^(YTKBaseRequest *request) {
}];
}
2015-02-11 21:42:29.001 YTKNetworkDemo[1070:55786] Add request: GetImageApi
2015-02-11 21:42:29.013 YTKNetworkDemo[1070:55786] Add request: GetUserInfoApi
2015-02-11 21:42:29.439 YTKNetworkDemo[1070:55786] Finished Request: GetUserInfoApi
2015-02-11 21:42:31.804 YTKNetworkDemo[1070:55786] respond22222 = {
data = (
{
dateline = "4\U5c0f\U65f6\U524d";
description = "";
favid = 127;
icon = "http://i.gugubaby.com/dz/static/image/feed/discuz.gif";
id = 286;
idtype = fid;
spaceuid = 0;
title = "\U6cf0\U5c71-\U4e94\U5cb3\U4e4b\U5c0a";
uid = 190;
url = "http://i.gugubaby.com/dz/BAPI/index.php?mod=forum&op=forumdisplay&ver=1.0&app=ios&fid=286";
},
{
dateline = "9\U5c0f\U65f6\U524d";
description = "";
favid = 124;
icon = "http://i.gugubaby.com/dz/static/image/feed/discuz.gif";
id = 319;
idtype = fid;
spaceuid = 0;
title = "\U5e16\U5b50\U6d4b\U8bd5";
uid = 190;
url = "http://i.gugubaby.com/dz/BAPI/index.php?mod=forum&op=forumdisplay&ver=1.0&app=ios&fid=319";
},
2015-02-11 21:42:31.836 YTKNetworkDemo[1070:55786] Request queue size = 1
2015-02-11 21:42:31.836 YTKNetworkDemo[1070:55786] Finished Request: GetImageApi
2015-02-11 21:42:31.837 YTKNetworkDemo[1070:55786] respond11111 = {
data = {
list = (
{
addtime = 1423117172;
dateline = "2015-02-05 14:19";
fid = 250;
fup = 214;
id = 4;
imgurl = "http://i.gugubaby.com/dz/BAPI/admins/resource/uploadfile/imgfile/";
lastpost = "";
lastposter = "";
lastsubject = "";
name = "\U957f\U5b59\U65e0\U5fcc";
replies = "";
views = "";
},
{
addtime = 1423117241;
dateline = "2015-02-05 14:20";
用YTKBatchRequest 来承载请求不走block 方法,求解决,具体看下面:
/// Send batch request
- (void)sendBatchRequest
{
GetImageApi *a = [[GetImageApi alloc] init];
GetUserInfoApi *b = [[GetUserInfoApi alloc] initWithType:@"all"];
// [a startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
// NSLog(@"respond11111 = %@",request.responseJSONObject);
//
// } failure:^(YTKBaseRequest *request) {
//
// }];
//
// [b startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
// NSLog(@"respond22222 = %@",request.responseJSONObject);
// } failure:^(YTKBaseRequest *request) {
//
// }];
YTKBatchRequest *batchRequest = [[YTKBatchRequest alloc] initWithRequestArray:@[a, b]];
[batchRequest startWithCompletionBlockWithSuccess:^(YTKBatchRequest *batchRequest) {
NSLog(@"succeed"); //此处不走
NSArray *requests = batchRequest.requestArray;
GetImageApi *a = (GetImageApi *)requests[0];
GetUserInfoApi *b = (GetUserInfoApi *)requests[1];
// deal with requests result ...
NSLog(@"a = \n%@, b = \n%@", a.responseJSONObject, b.responseJSONObject);
} failure:^(YTKBatchRequest *batchRequest) {
NSLog(@"failed");
}];
}
2015-02-11 22:00:22.616 YTKNetworkDemo[1106:60745] Add request: GetImageApi
2015-02-11 22:00:22.620 YTKNetworkDemo[1106:60745] Add request: GetUserInfoApi
2015-02-11 22:00:22.621 YTKNetworkDemo[1106:60745] Request queue size = 1
2015-02-11 22:00:22.626 YTKNetworkDemo[1106:60745] Request queue size = 0
2015-02-11 22:00:22.658 YTKNetworkDemo[1106:60745] Finished Request: (null)
2015-02-11 22:00:22.659 YTKNetworkDemo[1106:60745] Request queue size = 0
2015-02-11 22:00:22.659 YTKNetworkDemo[1106:60745] Finished Request: (null)
2015-02-11 22:00:22.659 YTKNetworkDemo[1106:60745] Request queue size = 0
加英文版Readme
因为使用requestUrl和requestArgument构造url所以,不知道到底请求的url是什么?想统一log出来看下
- (NSString *)requestUrl
{
return @"/auth/login";
}
- (id)requestArgument
{
return @{
@"Account": _account,
@"Password": _passwd
};
}
目前我的做法是在YTKNetworkAgent的addRequest:方法中打印出来
YTKRequestMethod method = [request requestMethod];
NSString *url = [self buildRequestUrl:request];
NSLog(@"url => %@", url);
为什么执行Demo后一片空白,storyboard中为什么都没显示?
怎么测?
我看了YTKNetwork的功能介绍里面有一条:
定义了一套插件机制,可以很方便地为 YTKNetwork 增加功能。猿题库官方现在提供了一个插件,可以在某些网络请求发起时,在界面上显示"正在加载"的 HUD。
但是在代码中并没有找到可以调用的接口,可以告诉我接口在哪里吗?
今儿我用YTKNetwork替换AFNetworking,发现用request.responseJSONObject取不到JSON数据,始终为nil,只能用request.responseString取到字符串,我的调用的接口返回的是JSON格式的数据。求解!
对于您分享的YTKNetwork网络库本人很感兴趣,所以就想尝试下所有的功能。但是,在尝试“支持方便地设置有相互依赖的网络请求的发送”这条功能时候遇到了如下几个问题。
1.如何设置根据请求A的返回内容去判断请求B还是请求C。(可能是我理解的不对,请点拨)
2.若请求A结束了,根据内容判断需要请求C,但是在请求C结束后需要再请求B,这样的功能YTKNetwork是否能够实现。
最后非常感谢您分享这个网络类,从中我学到了很多。
我设置了缓存时间,当我再次发送同样地请求的时候,判断缓存不存在,代码又发送了一次请求,不知道为什么....
[!] Unable to find a specification for YTKNetwork
?
弱弱的问下
为什么YTKBatchRequest 没有选择AF自己的 batchOfRequestOperations 那样使用 dispatch_group 而是使用计数
YTKChainRequest没有选择 addDependency
...
在缓存网络请求时,是根据请求的地址URL还是URL+请求参数来做缓存的呢?
比如有这种情况,有一个接口地址URL。有几个可选参数,可选参数不同返回的数据不同。
当通过这个URL请求数据,设置不同的参数,返回不同的数据。这时是根据什么来做缓存的呢?
遇到一个问题
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain"
现在暂时的解决方案是修改了YTKNetworkAgent
里的代码:
- (id)init {
self = [super init];
if (self) {
...
_manager = [AFHTTPRequestOperationManager manager];
...
[self addMissingContentTypes];
}
return self;
}
- (void)addMissingContentTypes {
NSArray *types = @[@"text/plain", otherContentType];
NSSet *contentTypes = [_manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:types];
_manager.responseSerializer.acceptableContentTypes = contentTypes;
}
但是修改源码终究是不太好,能改进一下,提供自定义responseSerializer.acceptableContentTypes的地方么?或者有现成的我没找到?求助 THANKS!😄
Please tell me how to use
感谢提供这样的一个框架,我想问的是我们一般是一个模块中会发起4,5个不同的api请求,那是否要创建4,5个不同名称的YTKRequest子类分别处理不同的网络请求吗?那是不是挺麻烦的,有能用一个类处理多个api地址的方法吗。
比如:UserApi类中,有/getUserInfo/:id ,/login/:name/:pwd, /changename/:id/newname 这样的3个不同的api类,那我要创建3个YTKRequest子类吗,能不能只创建一个YTKRequest类,在同一个类中处理这3个网络请求呢。
谢谢!
给个例子吧。demo里面貌似什么都没有的
您好,我想用咋的库实现多张图片按队列一张一张的上传,我想获得每一张图片的上传进度和总的图片组的上传进度,如何获得?
For the user who want to customize http header, we plan to expose AFNetworking's set header method.
你好,现在我的app中a模块中的请求地址是:http://192.168.1.1:8081/api1, 而我同一个app中b模块的请求地址是:http://192.168.1.2:8000/api2/getUserInfo ,那我怎么设置baseUrl呢?
谢谢。
比如说有一个接口UpdateInfoApi有三个参数:A、 B、C。只有A参数是必须参数,B、C参数是可选参数。能够实现这样的需求么?
实现了requestArgument
方法如下,这样是所有参数都传递了。能只传必须参数么?
- (id)requestArgument
{
return @{ @"UserId": @(_userId), @"B": @(xxx), @"C": @(yyy) };
}
YTKNetwork的**, 是不是每个请求都必需建立一个 YTKRequest 的子类。 如果是这样,有很多请求就建很多子类会不会很繁锁
Hi!@tangqiaoboy
如题
Jack
不好意思,看到你们这边也用了mantle,在这里想请教一下如何用mantle把复杂的model转换成json字符串,用jsonModel有个toJSONString方法是可以直接把model转换成json字符串的,包括一些承继和复杂的自定义对象都可以转换出来,但总找不到如果用mantle把model转成json字符串。只找到一个把model转成NSDictionary,
NSDictionary *jsonDictionary = [MTLJSONAdapter JSONDictionaryFromModel:notice];
请教如何能转换成json字符串呢。
不甚感激!
AFNetWorkingManager.responseSerializer.acceptableContentTypes在YTKNetwork中怎么设置呢?
请问当我在Api中设置了cacheTimeInSeconds值大于0,但我又想在有些时间强制从网络去取数据,那该怎么做呢?
看到有个 startWithoutCache方法,但是不支持Block,请问如何操作?是不是我需要在每次请求的时候动态设置 cacheTimeInSeconds 大于0或等于0来区分从网络取数据还是从缓存取数据?
如何在YTKNETWORK 中做TDD 和 BDD ??
感谢提供这么个好框架,已经用在我们的项目中,请问一下YTKNetwork有对应的Android版本吗,或有类似这样的提供缓存,封装的第三方库吗。
谢谢
麻烦 @lancy 在高级用法一节,增加 定制 buildCustomUrlRequest
相关的介绍,我已经放好 TODO 在ProGuide.md 里了。
上传单个文件,希望可以增加上传进度状态的回调,
我写了个类试用了一下,但是不知道如何接受请求到的json数据,返回的数据在responseString里,而不是responseJSONObject里:
//.h文件
@interface MeiTuAPI : YTKRequest
@Property (nonatomic, strong) NSNumber *siteID;
@Property (nonatomic, strong) NSNumber *typeID;
@Property (nonatomic, strong) NSNumber *styleID;
@Property (nonatomic, strong) NSNumber *pSize;
@Property (nonatomic, strong) NSNumber *page;
//.m文件
@implementation MeiTuAPI
-(instancetype)init{
if (self = [super init]) {
self.siteID = @1;
self.typeID = nil;
self.styleID = nil;
self.pSize = @20;
self.page = @1;
}
return self;
}
-(NSString *)baseUrl{
return BaseUrl;
}
-(NSString *)requestUrl{
return Url_amazingPicsList;
}
-(YTKRequestMethod)requestMethod{
return YTKRequestMethodPost;
}
//调用
(void)loadData{
MeiTuAPI *request = [[MeiTuAPI alloc]init];
request.siteID = @2;
request.pSize = @10;
[request startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
if (request.responseStatusCode != 0) {
NSLog(@"%@",request.responseString); //有数据
NSLog(@"%@",request.responseJSONObject); //返回nil
}
} failure:^(YTKBaseRequest *request) {
}];
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.