comparison 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
comparison
equal deleted inserted replaced
29:fabb3a5cdfc9 30:c0a6f7c66719
1 // 1 //
2 // ServerComms.m 2 // ServerComms.m
3 // sonicZoom 3 // httpPost
4 // 4 //
5 // Created by Robert Tubb on 21/02/2013. 5 // Created by Robert Tubb on 24/02/2013.
6 // 6 // Copyright (c) 2013 Robert Tubb. All rights reserved.
7 // 7 //
8 8
9 #import "ServerComms.h" 9 #import "ServerComms.h"
10 #import "eventLogger.h"
11
12 extern EventLogger eventLogger;
10 13
11 @implementation ServerComms 14 @implementation ServerComms
12 -(void)test{ 15 //
13 NSLog(@"hi"); 16
17
18 // asynchronous one
19 -(BOOL)doPostRequest:(NSString *)type withData:(NSString *)data{
20
21 if(self.requestInProgress){
22 return NO;
23 }
24 self.currentRequestType = type;
25 NSString *localServerURL = @"http://127.0.0.1:8080/testservice/";
26 NSString *webServerURL = @"http://www.isophonics.net/datacollector/";
27
28 NSString *urls = [webServerURL stringByAppendingString:type];
29 NSURL *url = [NSURL URLWithString:urls];
30
31
32 // request object
33 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
34
35 // the data
36 NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]];
37 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
38 // length of data
39 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
40
41 // request properties/header fields
42 [request setHTTPMethod:@"POST"];
43 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
44 [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
45 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
46 [request setHTTPBody:postData ];
47
48 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
49 self.requestInProgress = YES;
50 return YES;
51 // asynchronous one??
14 } 52 }
15 -(void)postRequest:(NSString *)msgbody{ 53 //--------------------------------------------------------------------
16 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 54
17 initWithURL:[NSURL 55 // schncronous
18 URLWithString:msgbody]]; 56 -(BOOL)doSyncPostRequest:(NSString *)type withData:(NSString *)data{
57 BOOL success;
19 58
59 NSString *localServerURL = @"http://127.0.0.1:8080/testservice/";
60 NSString *webServerURL = @"http://www.isophonics.net/datacollector/";
61
62 NSString *urls = [webServerURL stringByAppendingString:type];
63 NSURL *url = [NSURL URLWithString:urls];
64
65
66 // request object
67 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
68
69 // the data
70 NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]];
71 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
72 // length of data
73 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
74
75 // request properties/header fields
20 [request setHTTPMethod:@"POST"]; 76 [request setHTTPMethod:@"POST"];
21 [request setValue:@"text/xml" 77 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
22 forHTTPHeaderField:@"Content-type"]; 78 [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
79 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
80 [request setHTTPBody:postData ];
23 81
24 NSString *xmlString = @"<data><item>Item 1</item><item>Item 2</item></data>"; 82 NSURLResponse* response;
83 NSError* error = nil;
84 NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
85
86 NSString *responseDataString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
25 87
26 [request setValue:[NSString stringWithFormat:@"%d", 88 //NSString* responseDataString = [NSString stringWithUTF8String: ];
27 [xmlString length]] 89 NSLog(@"didRecieveDta %@", responseDataString);
28 forHTTPHeaderField:@"Content-length"]; 90 [self.data appendData:result];
29 91
30 [request setHTTPBody:[xmlString 92 if([responseDataString isEqualToString:@"testConnection:OK"]){
31 dataUsingEncoding:NSUTF8StringEncoding]]; 93
94 eventLogger.testConnectionOK();
95 success = true;
96 }else if([responseDataString isEqualToString:@"questionnaire:OK"]){
97 eventLogger.questionnaireOK();
98 success = true;
99 }else if([responseDataString isEqualToString:@"eventlog:OK"]){
100 // call eventLogger eventlogUploadOK
101 eventLogger.eventlogOK();
102 success = true;
103 }else{
104 success = false;
105 }
32 106
33 [[NSURLConnection alloc] 107 // else check error??
34 initWithRequest:request 108
35 delegate:self]; 109 // check result
110 self.requestInProgress = NO;
111 return success;
112 }
113 //-------------------------------------------------------------------------------------------------
114
115 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
116 [self.data setLength:0];
117 NSLog(@"didReceiveResponse : %@",response);
36 } 118 }
37 119
120 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
121 NSString *responseDataString = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
122
123 //NSString* responseDataString = [NSString stringWithUTF8String: ];
124 NSLog(@"didRecieveDta %@", responseDataString);
125 [self.data appendData:d];
126
127 if([responseDataString isEqualToString:@"testConnection:OK"]){
38 128
39 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 129 eventLogger.testConnectionOK();
40 NSLog(@"!!!!!!!!!!!!!!!!!!!!connection error"); 130 }else if([responseDataString isEqualToString:@"questionnaire:OK"]){
131 eventLogger.questionnaireOK();
132 }else if([responseDataString isEqualToString:@"eventlog:OK"]){
133 // call eventLogger eventlogUploadOK
134 eventLogger.eventlogOK();
135 }
136 // or?
137 /*
138 if([self.currentRequestType isEqualToString:@"testConnection"){
139
140 }
141 */
41 } 142 }
42 143
43 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 144 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
44 NSLog(@"!!!!!!!!!!!!!!!!!!!!didReceiveResponse"); 145 UIAlertView * av = [ [UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
146 message:[error localizedDescription]
147 delegate:nil
148 cancelButtonTitle:NSLocalizedString(@"OK", @"")
149 otherButtonTitles:nil ];
150 [av show];
151 NSLog(@"fail with error");
152 self.requestInProgress = NO;
153 // we won't know what kind of request method this was...
45 } 154 }
46 155
47 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 156 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
48 NSLog(@"!!!!!!!!!!!!!!!!!!!!didReceiveData"); 157 NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
158
159 // Do anything you want with it
160 NSLog(@"response text: %@",responseText);
161 self.requestInProgress = NO;
162
49 } 163 }
50 164
165 // Handle basic authentication challenge if needed
166 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
167 NSString *username = @"username";
168 NSString *password = @"password";
169
170 NSURLCredential *credential = [NSURLCredential credentialWithUser:username
171 password:password
172 persistence:NSURLCredentialPersistenceForSession];
173 [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
174 }
175 /*
176 - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse{
177
178 }
51 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{ 179 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
52 180
53 } 181 }
182 */
54 183
55 @end 184 @end