我有几个相互依赖的请求,我必须按顺序调用吗?有人可以用AFNetworking和反应可可给我一个例子吗?
例:
我正在将我的项目迁移到AFNetworking 2.0.使用AFNetworking 1.0时,我编写了代码来记录控制台中的每个请求/响应.这是代码:
-(AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request
success:(void (^)(AFHTTPRequestOperation *, id))success
failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
AFHTTPRequestOperation *operation =
[super HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject){
[self logOperation:operation];
success(operation, responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
failure(operation, error);
}];
return operation;
}
-(void)logOperation:(AFHTTPRequestOperation *)operation {
NSLog(@"Request URL-> %@\n\nRequest Body-> %@\n\nResponse [%d]\n%@\n%@\n\n\n",
operation.request.URL.absoluteString,
[[NSString alloc] initWithData:operation.request.HTTPBody encoding:NSUTF8StringEncoding],
operation.response.statusCode, operation.response.allHeaderFields, operation.responseString);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用AFNetworking 2.0做同样的事情,根据我的理解,这意味着使用NSURLSessionDataTask对象代替AFHTTPRequestOperation.这是我的镜头.
-(NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLResponse *, id, NSError *))completionHandler {
NSURLSessionDataTask *task = [super dataTaskWithRequest:request completionHandler:^(NSURLResponse …Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像从我的iPhone应用程序上传到S3,然后将S3网址存储回我的rails应用程序.我不应该在iOS应用程序中嵌入凭据,所以我采取的方法是:
aws-sdkgem生成并返回预先签名的URL 如何在S3中存储数据并允许用户使用rails API/iOS客户端以安全的方式访问?我尽力遵循我在网上找到的所有指示,但它不起作用,步骤3的结果返回错误401禁止.由于我是新手,我甚至不知道我做错了什么.
在第2步中,我的代码如下所示:
def getS3Url
s3 = AWS::S3.new(
:access_key_id => "MY S3 KEY",
:secret_access_key => "MY SECRET ACCESS KEY"
)
object = s3.buckets[params["bucket"]].objects[params["path"]]
@s3url = object.url_for(:write, { :expires => 20.minutes.from_now, :secure => true }).to_s
end
Run Code Online (Sandbox Code Playgroud)
从step2返回的url看起来像这样: https://s3.amazonaws.com/myapp-bucket-name/images/avatar/user1.png?AWSAccessKeyId=[access key id]&Expires=[expiration timestamp]&Signature=[Signature]
一旦我得到该URL,我尝试通过执行以下操作发布到它:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:[responseObject valueForKey:@"s3url"] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:jpegData name:@"file" fileName:self.filename mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, …Run Code Online (Sandbox Code Playgroud) 我有一个Web服务,我可以通过Postman实用程序成功发布调用.在Postman上的设置是
我无法使用代码使用Afnetworking进行相同的调用.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"body": @{@"email":@"email@gmail.com",@"name":@"myName"}};
[manager POST:@"http://myURL.com/user" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Run Code Online (Sandbox Code Playgroud)
我的猜测是我没有正确设置表单数据?
我有一个用于AFNetworking同步行为的用例(详情如下).我怎样才能做到这一点?
这是我的代码片段,我尽可能地简化了它.
我想返回成功响应,但我只得到nil(因为函数在调用块之前返回).
- (id)sendForUrl:(NSURL *)url {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
__block id response;
[manager GET:url.absoluteString parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
response = responseObject;
NSLog(@"JSON: %@", responseObject);
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
return response;
}
Run Code Online (Sandbox Code Playgroud)
详细信息 所以我需要这个同步行为的原因是因为我正在构建一个将在启动时引导应用程序的pod.引导捆绑命中服务并在本地保存一堆值.然后,这些值将用于当前会话,而不会更改.如果值发生变化,用户将获得奇怪的体验,因此重要的是我避免这种情况.
如果服务中断,那没关系.我们将使用默认值或查找上一个会话中的某些已保存值,但无论发生什么,我们都不希望用户的体验在会话中更改.
(这是A/B测试和实验的引擎 - 如果这有助于你"获得"用例).
我正在尝试使用AFNetworking第2版的第一步.
由于新版本的现有在线教程,如Ray Wenderlich的afnetworking-crash-course不再适用.
从AFNetworking 2迁移指南我得到了以下代码:
https://github.com/AFNetworking/AFNetwor ...迁移 - 指南
NSURL *URL = [NSURL URLWithString:@"http://example.com/foo.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:nil];
[operation start];
Run Code Online (Sandbox Code Playgroud)
这时我已经添加了导入
#import "AFNetworking.h"
Run Code Online (Sandbox Code Playgroud)
到.pch文件.
问题:我总是收到错误消息,AFJSONSerializer是未声明的.
我忘记了什么步骤?
最好的祝福
坦率
我在heroku创建了一个测试应用程序(使用scaffold),我在这个heroku应用程序中构建了一个iOS客户端(使用AFNetworking 2).我试图使用iOS应用程序从heroku中删除记录,但它无法正常工作.我从服务器收到422状态错误.
查看heroku日志,我发现服务器声称拥有CSRF令牌.所以我尝试在我的iOS客户端上使用此代码执行此操作:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer new];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
[manager DELETE:contact.url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Response: %@", [operation description]) ;
if (block) {
block(error);
}
NSLog(@"Error: %@", error);
}];
Run Code Online (Sandbox Code Playgroud)
它没用.
如何在AFHTTPRequestOperationManager上将CSRF令牌添加到http标头中?
在AFNetworking的早期版本中,如果我必须创建自己的自定义客户端,那么我只需从AFHTTPClient继承并创建我的方法.在AFNetworking 2.0中,我相信我需要从AFHTTPSessionManager继承.
@interface MyCustomClient : AFHTTPSessionManager
{
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我需要发送请求作为肥皂.这意味着HTTP Body将是soap,HTTP HEADERS将是text/xml.
假设我有一个变量,其中包含我需要发送到服务器的整个肥皂体.
NSString *soapBody = @"Soap body";
Run Code Online (Sandbox Code Playgroud)
使用从AFHTTPSessionManager继承的上面定义的自定义类,如何将soap主体设置为Request HTTPBody.
如果无论如何都要从AFHTTPSessionManager内部访问NSURLRequest,那么我可以简单地做setHTTPBody,但似乎没有?
我希望我现在有意义!
我正在尝试下面的代码,但它给出了错误:
{
NSURL *url = [NSURL URLWithString:@"http://ielmo.xtreemhost.com/array.php"];
NSURLRequest *urlRequest =[[NSURLRequest alloc]initWithURL:url];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
_imV.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[requestOperation start];
}
Run Code Online (Sandbox Code Playgroud)
请帮我解决"Request failed: unacceptable content-type: text/html"错误.
我正在使用AFNetworking从我的服务器下载文件.它工作正常.但我有一个问题:当我向上或向下滚动时,我的ProgressView更新了错误的单元格(UI,而不是数据).这是我的代码:
我的细胞:
AFHTTPRequestOperation *operation;
@property (weak, nonatomic) IBOutlet DACircularProgressView *daProgressView;
- (IBAction)pressDown:(id)sender {
AFAPIEngineer *apiEngineer = [[AFAPIEngineer alloc] initWithBaseURL:[NSURL URLWithString:AF_API_HOST]];
operation = [apiEngineer downloadFile:(CustomObject*)object withCompleteBlock:^(id result) {
} errorBlock:^(NSError *error) {
}];
__weak typeof(self) weakSelf = self;
apiEngineer.afProgressBlock = ^(double progress, double byteRead, double totalByToRead) {
[weakSelf.daProgressView setProgress:progress animated:YES];
};
}
- (void)setDataForCell:(id)object{
}
Run Code Online (Sandbox Code Playgroud)
我的桌子:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomCell class])];
cell.backgroundColor = [UIColor clearColor];
CustomObject *aObject = [listObject objectAtIndex:indexPath.row];
[cell setDataForCell: aObject];
return …Run Code Online (Sandbox Code Playgroud) afnetworking-2 ×10
ios ×6
afnetworking ×4
amazon-s3 ×1
asynchronous ×1
heroku ×1
ios7 ×1
ipad ×1
iphone ×1
objective-c ×1
uitableview ×1
xcode ×1