【问题标题】:Translating from ASIHTTPRequest to AFNetworking , on iOS在 iOS 上从 ASIHTTPRequest 转换为 AFNetworking
【发布时间】:2012-07-02 13:50:45
【问题描述】:

我想要做的是将一个 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 的确切代码。有任何想法吗?? 感谢您阅读我的帖子:)

【问题讨论】:

    标签: ios asihttprequest afnetworking


    【解决方案1】:
    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);
              }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多