我想要做的是将一个 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/