ios - AFNetworking 2 Upload Plist Data to Apache Server -
i writing sample app test how afnetworking can used replacement asihttplib.. old library made simple upload file apache server (provided user has write access url/directory). no other server side support used..
this code has problem, have not pinpointed it: executing method reports upload success, plist file on cloud side not change…
-(void)uploadreminders:(nsarray*)reminders { nslog(@"appdelegate synch reminders cloud"); //nslog(@"data: %@", reminders); [self persistreminders:reminders atcustompath:nil]; nsstring *cachedirectorypath = [self cachesdirectorypath]; nsstring *urlstring = [nsstring stringwithformat:@"%@/%@",kstandardcloudareaaccessurl,kstandardlocalcacheplistfile]; //nslog(@"url: %@", urlstring); nsstring *remindersplistfile = [nsstring stringwithformat:@"%@/%@",cachedirectorypath,kstandardlocalcacheplistfile]; //nslog(@"filepath: %@",remindersplistfile); nsurlcredential *defaultcredential = [nsurlcredential credentialwithuser:kstandardcloudareaaccessusername password:kstandardcloudareaaccessuserpw persistence:nsurlcredentialpersistencenone]; /**/ nsurl *url = [nsurl urlwithstring:urlstring]; nsstring *host = [url host]; nsinteger port = [[url port] integervalue]; nsstring *protocol = [url scheme]; nsurlprotectionspace *protectionspace = [[nsurlprotectionspace alloc] initwithhost:host port:port protocol:protocol realm:nil authenticationmethod:nsurlauthenticationmethodhttpbasic]; nsurlcredentialstorage *credentials = [nsurlcredentialstorage sharedcredentialstorage]; [credentials setdefaultcredential:defaultcredential forprotectionspace:protectionspace]; nsurlsessionconfiguration *configuration = [nsurlsessionconfiguration defaultsessionconfiguration]; [configuration seturlcredentialstorage:credentials]; [configuration sethttpadditionalheaders: @{@"accept": @"text/plain"}]; afhttpsessionmanager *manager = [[afhttpsessionmanager alloc] initwithsessionconfiguration:configuration]; [manager.securitypolicy setallowinvalidcertificates:yes]; manager.responseserializer = [afpropertylistresponseserializer serializer]; manager.responseserializer.acceptablecontenttypes = [nsset setwithobject:@"text/plain"]; nsurl *url = [nsurl urlwithstring:urlstring]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; nsurl *filepath = [nsurl fileurlwithpath:remindersplistfile]; nsurlsessionuploadtask *uploadtask = [manager uploadtaskwithrequest:request fromfile:filepath progress:nil completionhandler:^(nsurlresponse *response, id responseobject, nserror *error) { if (error) { nslog(@"upload error: %@", error); } else { //nslog(@"upload success"); nslog(@"upload success: %@ %@", response, responseobject); } }]; [uploadtask resume]; }
the console shows this:
upload success: <nshttpurlresponse: 0x8d4ec50> { url: https://[.....]/codetests/reminders/reminders.plist } { status code: 200, headers { "accept-ranges" = bytes; connection = "keep-alive"; "content-length" = 1324; "content-type" = "text/plain"; date = "mon, 10 mar 2014 14:48:13 gmt"; etag = "\"3f06e5-52c-4f430180dae80\""; "keep-alive" = "timeout=5, max=100"; "last-modified" = "sun, 09 mar 2014 17:48:26 gmt"; server = "apache/2.2.17 (unix) mod_ssl/2.2.17 openssl/0.9.7l dav/2"; } } { reminders = ( { completed = 1; created = "2014-03-09 17:47:41 +0000"; description = ""; title = reminder; updated = "2014-03-09 17:47:41 +0000"; }, { completed = 1; created = "2014-03-08 09:47:58 +0000"; description = "orza!!! ma orza in fretta... ah: funziona? ebbene, s\u00ec! o no?\n"; title = "reminder orza"; updated = "2014-03-08 11:39:43 +0000"; }, { completed = 0; created = "2014-03-07 11:09:59 +0000"; description = "whatever like; , of course can make quite long.\n\nyeooww..\nreally long!\n\n\n\n"; title = "reminder a"; updated = "2014-03-08 11:34:24 +0000"; } ); version = "1.0"; }
the catch when reopen app, jumps test reminders did manually put on server: reminders.plist gets never changed.
thanks!
i'm assuming nslog
of responseobject
confirming plist received server. if so, may eliminate above "upload" code source of problem. may want inspect data on server (not through app, manually inspect yourself) , see whether new list of reminders there or not. seems there couple of possible logical possibilities:
if updated data not there, @ server's "save" logic, appear failing.
if there, should @ client's "retrieve" logic. wonder, example, if app caching responses requests, , when attempt download again, perhaps you're getting cached original response. i'd try turning off caching.
in these cases, tool charles can useful, can inspect requests , responses you're getting. can helpful in narrowing down precisely problem occurring.
taking closer @ request, notice you're not specifying request type. have thought request post
:
nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; [request sethttpmethod:@"post"]; [request setvalue:@"text/plain" forhttpheaderfield:@"accept"]; // i'd set `accept` here @ request, not @ `nsurlsessionconfiguration`
having said that, given web service reporting plist data @ you, have inferred received on basis of evidence shared far. maybe failure make request post
request means web service concluded didn't need save anything.
generally, when interfacing web service, rather tweaking request, have above, i'd encourage post data using 1 of standard afhttpsessionmanager
variations of post
method (one multipart/form-data
requests, other other requests). can't figure out server doing on basis of you've provided far (e.g. makes no sense server sending body of request @ all; makes no sense server appear have received request, doesn't , doesn't report error; etc.). maybe try making request post
request , see if fixes it. if not, run charles on old asihttp source code, , you'll see precisely old request looks , should able reproduce afnetworking.
Comments
Post a Comment