objective c - Change NSURLConnection from sendSynchronousRequest to sendAsynchronousRequest? -
my code nsurlconnection sendsynchronousrequest works fine how can change async request? tried lot nothing work.
if request empty i´ll empty array [[]] . how can catch alert message?
please ...
[uiapplication sharedapplication].networkactivityindicatorvisible = yes; nsstring *urlstring = @"http://www.xyz.at/sample.php"; nsurl *url = [nsurl urlwithstring:urlstring]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:url]; [request sethttpmethod:@"post"]; nsmutabledata *body = [nsmutabledata data]; nsstring *postwerte = [nsstring stringwithformat:@"id=%@", self.textfeld.text]; [body appenddata:[postwerte datausingencoding:nsutf8stringencoding]]; [request sethttpbody:body]; nserror *error = nil; nsdata *returndata = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:&error]; nslog(@"error: %@", error.description); nsstring *returnstring = [[nsstring alloc] initwithdata:returndata encoding:nsutf8stringencoding]; const char *convert = [returnstring utf8string]; nsstring *responsestring = [nsstring stringwithutf8string:convert]; nsmutablearray *meinergebnis = [responsestring jsonvalue]; nsstring *cycle = @""; nsstring *kopfdaten = [nsstring stringwithformat:@"sendungsart: %@\r\ngewicht: %@ kg\r\n\r\n", [[meinergebnis objectatindex:0] objectforkey:@"parceltypedescription"], [[meinergebnis objectatindex:0] objectforkey:@"weight"]]; cycle = [cycle stringbyappendingstring:kopfdaten]; for(int = 1; < meinergebnis.count; i++) { nsstring *myvalue = [nsstring stringwithformat:@"%@ plz: %@\r\nstatus: %@\r\n\r\n", [[meinergebnis objectatindex:i] objectforkey:@"eventtimestamp"], [[meinergebnis objectatindex:i] objectforkey:@"eventpostalcode"], [[meinergebnis objectatindex:i] objectforkey:@"parceleventreasondescription"]]; cycle = [cycle stringbyappendingstring:myvalue]; } self.ergebnis.text = [nsstring stringwithformat:@"%@", cycle]; [uiapplication sharedapplication].networkactivityindicatorvisible = no; [self.textfeld resignfirstresponder];
you could:
create
nsoperationqueue
, ,call
sendasynchronousrequest
, placing ofnsdata
processing code inside completion block.
thus, instead of:
nsurlresponse *response = nil; nserror *error = nil; nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; // process resulting `data`
use:
nsoperationqueue *queue = [[nsoperationqueue alloc] init]; [nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) { // process resulting `data` }];
alternatively, implement nsurlconnectiondatadelegate
methods. more information on that, see using nsurlconnection section of url loading system programming guide.
you "if request empty": assume mean "if data returned empty". , [[]]
. if that's you're getting, sounds array 1 item (which itself, empty array). or []
(which empty array)? or nil
?
i'm going assume data returned []
, empty array.
i'd suggest consider using nsjsonserialization
, built in json parser, can use jsonvalue
if want.
finally, implementation skipping first entry (nsarray
uses zero-based index). i'm assuming unintentional.
nsoperationqueue *queue = [[nsoperationqueue alloc] init]; [nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^(nsurlresponse *response, nsdata *data, nserror *error) { if (error) { nslog(@"%s: sendasynchronousrequest error: %@", __function__, error); return; } nserror *parseerror; nsarray *meinergebnis = [nsjsonserialization jsonobjectwithdata:data options:0 error:&parseerror]; if (parseerror) { nslog(@"%s: jsonobjectwithdata error: %@", __function__, parseerror); return; } if ([meinergebnis count] == 0) { nslog(@"%s: meinergebnis empty", __function__); return; } (nsdictionary *dictionary in meinergebnis) { // process each dictionary entry in meinergebnis } // etc. }];
Comments
Post a Comment