3

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

4

1 回答 1

3
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);
          }];
于 2012-07-03T18:48:14.400 回答