ios - 在 iOS 上从 ASIHTTPRequest 转换为 AFNetworking

标签 ios asihttprequest afnetworking

我想要做的是将一个 json 文件发布到我的服务器。我终于使用 ASIHTTPRequest 做到了,我的代码是:

NSURL *url = [NSURL URLWithString:@"http://foo.foo.foo"];
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            [request setRequestMethod:@"PUT"];

            [request addRequestHeader:@"Authorization" value:@"Basic dslfkdsfdsflsdkfslkgflksflksdlfkg"]; 
            [request addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];


            [request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]; 
            [request startSynchronous];

            NSLog(@"%@", [request responseString]);   

我现在需要的是将上面的代码准确翻译成 AFNetworking。有任何想法吗?? 感谢您阅读我的帖子:)

最佳答案

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://foo.foo.foo"]];
client.parameterEncoding = AFJSONParameterEncoding;
[client registerHTTPOperationClass:[AFJSONRequestionOperation class]];
[client setAuthorizationHeaderWithUsername:... password:...];
[client putPath:@"/path/to/resource" parameters:(_your JSON object as a dictionary_) 
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
              NSLog(@"%@", [request responseString]);
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
          }];

关于ios - 在 iOS 上从 ASIHTTPRequest 转换为 AFNetworking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295058/

相关文章:

ios - 如何知道ASIFormDataRequest完成了?

ios - AFNetworking - 获取响应内容长度

ios - Objective-C中基于触摸的图像处理

ios - 从 ASIHTTPRequest 转换为 AFNetworking 的最佳实践?

ios - 无法在自定义 UIView 上单击 UIButton

objective-c - ASIHTTPRequest 错误错误的 URL

ios - 域=NSURLError域代码=-1021 "request body stream exhausted"

iphone - 如何获取在后台运行的 AFNetworking AFURLConnectionOperation 的进度?

android - 查询以设置要使用的图像大小

ios - 为什么我的 for 循环不正确地增加 strokeColor