iphone - NSURLConnection doen't return data where dataWithContentsOfURL does -
i have application , used json info datawithcontentsofurl
. because need asynchrone fashion. utilize nsurlconnection
handle don't receive useful info statuscode 200 in didreceiveresponse
method didreceivedata
never called. , @ connectiondidfinishdownloading
destinationurl
returns null
.
i have no thought wat couses problem , appreciate help.
the delegate
#import "networktoguidelegate.h" @implementation networktoguidelegate @synthesize data; @synthesize caller; - (id) init: (sel) pointer :(nsobject *) c; { dowhendone = pointer; self.caller = c; info = [[nsmutabledata alloc]init]; homecoming self; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { nsstring *responsetext = [[nsstring alloc] initwithdata:self.data encoding:nsutf8stringencoding]; nslog(@"%@",responsetext); } - (void)connectiondidfinishdownloading:(nsurlconnection *)connection destinationurl:(nsurl *)destinationurl { nslog(@"post download finished"); info = [nsdata datawithcontentsofurl:destinationurl]; nslog(@"data: %@",data); nslog(@"urlconnection %@", connection.currentrequest); #pragma clang diagnostic force #pragma clang diagnostic ignored "-warc-performselector-leaks" [caller performselector:dowhendone withobject:data]; #pragma clang diagnostic pop } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [self.data setlength:0]; nshttpurlresponse *resp= (nshttpurlresponsae *) response; nslog(@"got responce status %d",[resp statuscode]); } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)d { nslog(@"data recieved %@",d); [self.data appenddata:d]; } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { [[[uialertview alloc] initwithtitle:nslocalizedstring(@"error", @"") message:[error localizeddescription] delegate:nil cancelbuttontitle:nslocalizedstring(@"ok", @"") otherbuttontitles:nil] show]; nslog(@"failed"); } // handle basic authentication challenge if needed - (void)connection:(nsurlconnection *)connection didreceiveauthenticationchallenge:(nsurlauthenticationchallenge *)challenge { nsstring *username = @"username"; nsstring *password = @"password"; nsurlcredential *credential = [nsurlcredential credentialwithuser:username password:password persistence:nsurlcredentialpersistenceforsession]; [[challenge sender] usecredential:credential forauthenticationchallenge:challenge]; } @end
the call
networktoguidelegate *nwgd = [[networktoguidelegate alloc] init:@selector(login:) :self]; nsstring *stringurl = [nsstring stringwithformat:@"%@%@%@%@", dk.baseurl, @"menu?code=",dk.logincode,@"&v=1"]; nsurl *url = [nsurl urlwithstring:stringurl]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; nsurlconnection *urlconnection = [[nsurlconnection alloc] initwithrequest:request delegate:nwgd]; [urlconnection start];
i'll add together phone call happens on main thread
actually there lot of points create reply of question , there many questions on so itself
go through nice post
and must read doc apple itself.(contains code samples , explanations)
hope solve problems you.
edit
-(void)startaconnection { // create request. nsurlrequest *therequest=[nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://www.apple.com/"] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0]; // create connection request // , start loading info nsurlconnection *theconnection=[[nsurlconnection alloc] initwithrequest:therequest delegate:self]; if (theconnection) { // create nsmutabledata hold received data. // receiveddata instance variable declared elsewhere. receiveddata = [nsmutabledata data]; } else { // inform user connection failed. } } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { // method called when server has determined // has plenty info create nsurlresponse. // can called multiple times, illustration in case of // redirect, each time reset data. // receiveddata instance variable declared elsewhere. [receiveddata setlength:0]; } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { // append new info receiveddata. // receiveddata instance variable declared elsewhere. [receiveddata appenddata:data]; } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { // inform user nslog(@"connection failed! error - %@ %@", [error localizeddescription], [[error userinfo] objectforkey:nsurlerrorfailingurlstringerrorkey]); } - (void)connectiondidfinishloading:(nsurlconnection *)connection { nslog(@"succeeded! received %d bytes of data",[receiveddata length]); }
call startaconnection start process
note: dont forget <nsurlconnectiondelegate>
in .h
happy coding :)
iphone json nsurlconnection nsdata
No comments:
Post a Comment