通过AFNetworking发布请求

时间:2012-09-29 05:34:50

标签: iphone ios afnetworking

我是编程新手,特别是在网络方面。所以现在我正在创建与Instagram交互的应用程序。在我的项目中,我使用AFNetworking。我在这里看到了他们的文档和许多例子。我还不明白如何向Instagram API发送POST请求。请问您能给我一些真实的代码示例或我可以阅读的有关如何执行此操作的内容吗?请帮忙。我试图像这样提出请求,它没有错误,也没有响应。它什么都没有:(

(IBAction)doRequest:(id)sender{

NSURL *baseURL = [NSURL URLWithString:@"http://api.instagram.com/"];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient defaultValueForHeader:@"Accept"];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        user_token, @"access_token",
                        nil];

[httpClient postPath:@"/feed" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // reponseObject will hold the data returned by the server.
    NSLog(@"data: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error retrieving data: %@", error);
}];


NSLog(@"click!!");
}

1 个答案:

答案 0 :(得分:4)

很少有事情要关心。 Instagram API返回JSON,因此您可以使用AFJSONRequestOperation,它将返回已经解析过的NSDictionary Instagram API说:

  

所有端点只能通过https访问,位于   api.instagram.com。

您应该对baseURL进行更改。

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:yourURL];
NSURLRequest *request = [client requestWithMethod:@"POST"
                                             path:@"/your/path"
                                       parameters:yourParamsDictionary];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation
 JSONRequestOperationWithRequest:request
 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
    // Do something with JSON
}
 failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
    // 
}];

// you can either start your operation like this 
[operation start];

// or enqueue it in the client default operations queue.
[client enqueueHTTPRequestOperation:operation];
相关问题