ios - Preparing a GET/POST request to fetch data on iPhone -
i trying fetch data in json format search word 'cancer'.
but can't figure out how call websvice, tried few things not working, can me in this.
below api should calling https://api.justgiving.com/docs/resources/v1/search/fundraisersearch
clicking following url desired data in browser. https://api.justgiving.com/2be58f97/v1/fundraising/search?q=cancer
apikey = 2be58f97
here code using:
nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; nsurl *requesturl = [nsurl urlwithstring:@"https://api.justgiving.com/2be58f97/v1/fundraising/search"]; [request seturl:requesturl]; [request sethttpmethod:@"get"];
nsstring *boundary = @"---------------------------14737809831466499882746641449"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary]; [request addvalue:contenttype forhttpheaderfield: @"content-type"]; nsmutabledata *body = [nsmutabledata data]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n", boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[@"content-disposition: form-data; name=\"q\"\r\n\r\n" datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"%@",searchtext] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@--\r\n", boundary] datausingencoding:nsutf8stringencoding]]; [request sethttpbody:body]; [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) { nslog(@"error = %@",error.localizeddescription); if(error.localizeddescription == null) { nsstring *returnstring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nslog(@"response >>>>>>>>> %@",returnstring); } else { nsstring *returnstring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nslog(@"response >>>>>>>>> %@",returnstring); } }];
-(afhttpclient *) gethttpclient{ afhttpclient *httpclient = [[afhttpclient alloc]initwithbaseurl:[nsurl urlwithstring:kbaseurl]]; httpclient.parameterencoding = afjsonparameterencoding; [httpclient setdefaultheader:@"accept" value:@"application/json"]; [httpclient registerhttpoperationclass:[afjsonrequestoperation class]]; return httpclient; } //this how should call -(void) callapi{ afhttpclient *httpclient = [self gethttpclient]; nsmutableurlrequest *request = [httpclient requestwithmethod:@"get" path:method parameters:querystrdictionary];// querystringdictionary contains value of q=? stuff afjsonrequestoperation *operation = [afjsonrequestoperation jsonrequestoperationwithrequest:request success:^(nsurlrequest *request, nshttpurlresponse *response, id json) { //you have response here want. [self processresponsewith:json having:successblock andfailuerblock:failureblock]; } failure:^(nsurlrequest *request, nshttpurlresponse *response, nserror *error, id json) { //your request failed check error detail failureblock(error); }]; nsoperationqueue *queue = [[nsoperationqueue alloc] init] ; [queue addoperation:operation]; }
Comments
Post a Comment