gpt4 book ai didi

iphone - 使用 AFNetworking 发布请求

转载 作者:行者123 更新时间:2023-11-28 19:15:08 27 4
gpt4 key购买 nike

我是编程新手,尤其是在网络方面。所以现在我正在创建与 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!!");
}

最佳答案

需要注意的事情很少。Instagram API 返回 JSON,因此您可以使用 AFJSONRequestOperation,它会返回一个已解析的 NSDictionary。
Instagram API 表示:

All endpoints are only accessible via https and are located at 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];

关于iphone - 使用 AFNetworking 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650234/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com