Mercurial > hg > soniczoomios
diff ServerComms.mm @ 30:c0a6f7c66719
Josh M test "in house" version 0.1
| author | Robert Tubb <rt300@eecs.qmul.ac.uk> |
|---|---|
| date | Wed, 27 Feb 2013 11:39:07 +0000 |
| parents | fabb3a5cdfc9 |
| children | 23ef179c3748 |
line wrap: on
line diff
--- a/ServerComms.mm Fri Feb 22 17:41:38 2013 +0000 +++ b/ServerComms.mm Wed Feb 27 11:39:07 2013 +0000 @@ -1,55 +1,184 @@ // // ServerComms.m -// sonicZoom +// httpPost // -// Created by Robert Tubb on 21/02/2013. -// +// Created by Robert Tubb on 24/02/2013. +// Copyright (c) 2013 Robert Tubb. All rights reserved. // #import "ServerComms.h" +#import "eventLogger.h" + +extern EventLogger eventLogger; @implementation ServerComms --(void)test{ - NSLog(@"hi"); +// + + +// asynchronous one +-(BOOL)doPostRequest:(NSString *)type withData:(NSString *)data{ + + if(self.requestInProgress){ + return NO; + } + self.currentRequestType = type; + NSString *localServerURL = @"http://127.0.0.1:8080/testservice/"; + NSString *webServerURL = @"http://www.isophonics.net/datacollector/"; + + NSString *urls = [webServerURL stringByAppendingString:type]; + NSURL *url = [NSURL URLWithString:urls]; + + + // request object + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; + + // the data + NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]]; + NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; + // length of data + NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; + + // request properties/header fields + [request setHTTPMethod:@"POST"]; + [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; + [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; + [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; + [request setHTTPBody:postData ]; + + NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; + self.requestInProgress = YES; + return YES; + // asynchronous one?? } --(void)postRequest:(NSString *)msgbody{ - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] - initWithURL:[NSURL - URLWithString:msgbody]]; +//-------------------------------------------------------------------- + +// schncronous +-(BOOL)doSyncPostRequest:(NSString *)type withData:(NSString *)data{ + BOOL success; + NSString *localServerURL = @"http://127.0.0.1:8080/testservice/"; + NSString *webServerURL = @"http://www.isophonics.net/datacollector/"; + + NSString *urls = [webServerURL stringByAppendingString:type]; + NSURL *url = [NSURL URLWithString:urls]; + + + // request object + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; + + // the data + NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]]; + NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; + // length of data + NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; + + // request properties/header fields [request setHTTPMethod:@"POST"]; - [request setValue:@"text/xml" - forHTTPHeaderField:@"Content-type"]; + [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; + [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; + [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; + [request setHTTPBody:postData ]; - NSString *xmlString = @"<data><item>Item 1</item><item>Item 2</item></data>"; + NSURLResponse* response; + NSError* error = nil; + NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; + + NSString *responseDataString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; - [request setValue:[NSString stringWithFormat:@"%d", - [xmlString length]] - forHTTPHeaderField:@"Content-length"]; + //NSString* responseDataString = [NSString stringWithUTF8String: ]; + NSLog(@"didRecieveDta %@", responseDataString); + [self.data appendData:result]; - [request setHTTPBody:[xmlString - dataUsingEncoding:NSUTF8StringEncoding]]; + if([responseDataString isEqualToString:@"testConnection:OK"]){ + + eventLogger.testConnectionOK(); + success = true; + }else if([responseDataString isEqualToString:@"questionnaire:OK"]){ + eventLogger.questionnaireOK(); + success = true; + }else if([responseDataString isEqualToString:@"eventlog:OK"]){ + // call eventLogger eventlogUploadOK + eventLogger.eventlogOK(); + success = true; + }else{ + success = false; + } - [[NSURLConnection alloc] - initWithRequest:request - delegate:self]; + // else check error?? + + // check result + self.requestInProgress = NO; + return success; +} +//------------------------------------------------------------------------------------------------- + +- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { + [self.data setLength:0]; + NSLog(@"didReceiveResponse : %@",response); } +- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { + NSString *responseDataString = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]; + + //NSString* responseDataString = [NSString stringWithUTF8String: ]; + NSLog(@"didRecieveDta %@", responseDataString); + [self.data appendData:d]; + + if([responseDataString isEqualToString:@"testConnection:OK"]){ -- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ - NSLog(@"!!!!!!!!!!!!!!!!!!!!connection error"); + eventLogger.testConnectionOK(); + }else if([responseDataString isEqualToString:@"questionnaire:OK"]){ + eventLogger.questionnaireOK(); + }else if([responseDataString isEqualToString:@"eventlog:OK"]){ + // call eventLogger eventlogUploadOK + eventLogger.eventlogOK(); + } + // or? + /* + if([self.currentRequestType isEqualToString:@"testConnection"){ + + } + */ } -- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ - NSLog(@"!!!!!!!!!!!!!!!!!!!!didReceiveResponse"); +- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { + UIAlertView * av = [ [UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"") + message:[error localizedDescription] + delegate:nil + cancelButtonTitle:NSLocalizedString(@"OK", @"") + otherButtonTitles:nil ]; + [av show]; + NSLog(@"fail with error"); + self.requestInProgress = NO; + // we won't know what kind of request method this was... } -- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ - NSLog(@"!!!!!!!!!!!!!!!!!!!!didReceiveData"); +- (void)connectionDidFinishLoading:(NSURLConnection *)connection { + NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]; + + // Do anything you want with it + NSLog(@"response text: %@",responseText); + self.requestInProgress = NO; + } +// 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]; +} +/* +- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse{ + +} - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{ } +*/ @end
