Posta richiesta con Afnetworking
-
11-12-2019 - |
Domanda
Sono newbie in programmazione, specialmente nel lato della rete.Quindi ora sto creando un'app per interagire con Instagram.Nel mio progetto uso la lavorazione dell'afnet.Ho visto la loro documentazione e molti esempi qui.E non capisco ancora come ottenere la richiesta di post a API Instagram.Per favore, potresti darmi il vero esempio di codice o qualcosa di dove posso leggere su come fare questa operazione?Per favore aiuto.Ho provato a fare richiesta in questo modo, non dà errori e nessuna risposta.Non dà nulla: (
(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!!");
}
. Soluzione
Poche cose a cui importa.
Instagram API restituisce JSON in modo da poter utilizzare AfjsonRequestOperation che restituirà un nsdictionary già analizzato.
L'API Instagram dice che:
.Tutti gli endpoint sono accessibili solo tramite HTTPS e si trovano a API.Instagram.com.
Dovresti effettuare una modifica al tuo 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];