2012-09-29 34 views
0

發佈採購信息我在編程的新手,尤其是在網絡側。所以現在我正在創建與Instagram互動的應用程序。在我的項目中,我使用AFNetworking。我在這裏看到了他們的文檔和許多例子。我不明白如何獲得POST API請求到Instagram API。請ü可以給我真正的代碼示例或東西在那裏我可以瞭解如何執行此操作?請幫忙。我試圖提出這樣的要求,它沒有提供任何錯誤,也沒有任何迴應。它提供了什麼:(與AFNetworking

(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!!"); 
} 

回答

4

幾件事情要關心 Instagram的API返回JSON這樣你就可以使用AFJSONRequestOperation它會返回一個已經被解析的NSDictionary
的Instagram的API說:。

所有端點只能通過HTTPS訪問,位於 api.instagram.com。

你應該做出改變你的基地URL。

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]; 
+0

非常感謝,弗洛裏安!當我接近我的Mac時,我會嘗試它。還有一兩件事:在請求參數「路徑」 - 是路徑設備上JSON或本地路徑? – pash3r

+0

這是附加到你提供的AFHTTPClient基本URL路徑。例如,通過爲路徑指定「/ users/self/feed」,請求URL將爲「https:// api.instagram.com/users/self/feed」。 – florian

+0

你是我的救命恩人!!!!!!!!!非常感謝你!!!! – pash3r