0

I have an app which is connected to a database via php, Im trying to send some large data to the database e.g. a list of coordinates

"37.351701,-122.105898",
"37.351945,-122.106109",
"37.352183,-122.106338",
"37.352409,-122.106568"

a string like this of 4 lines gets added no problem, a string of 150 lines also gets added but around the 200 lines mark its not, and the rest of the data Im trying to post with it also gets lost. I have tested this with php by creating .txt and adding the data to it and its not reaching the the php file if its over 200 lines, but 150 is fine again.

Im taking the coordinates and putting them into an array,

if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
    (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))

    self.trackCoord = [NSString stringWithFormat:@"%f,%f",newLocation.coordinate.latitude, newLocation.coordinate.longitude];

        if (!self.trackArray) {
        self.trackArray = [[NSMutableArray alloc] init];
    }
    [self.trackArray addObject:self.trackCoord];

the array get passed into another array trackArrayList etc and in this method im posting to the php file.

-(void) shareRoute:(NSString*) to withName:(NSString *) from withName:(NSString *) name withName:(NSString *) type withName:(NSString *) duration withName:(NSMutableArray *) trackArrayList{

if (from != nil && to != nil && name != nil){

    NSMutableString *postString = [NSMutableString stringWithString:shareURL];

    [postString appendString:[NSString stringWithFormat:@"?%@=%@", Sfrom, from]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", Sto, to]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", Sname, name]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", Stype, type]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", Sduration, duration]];
    [postString appendString:[NSString stringWithFormat:@"&%@=%@", StrackArray, trackArrayList]];

    [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
    [request setHTTPMethod:@"POST"];

    postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];        
    }

}

I have tried writing [postString appendString:[NSString stringWithFormat:@"&%@=%@", StrackArray, trackArrayList]]; in different variations, NSMutableString stringWithFormat etc but same result.

Is the string not big enough to hold so many lines or is the app running out of memory before being able to post.

Any suggestions to what could be causing this, or any others ways to write the appendString.

Ernie
  • 89
  • 1
  • 11
  • Why aren't you using `appendFormat:`? – rmaddy Mar 11 '14 at 20:40
  • The number of characters in the url might be too much for your web server. 200 strings in the array are around 5000 characters in the URL. Additionally: by using `[array description]` (which is called by the %@ format specifier) you are basically relying on implementation details. You should convert the array to a well defined format (e.g. JSON) and send it as the body of the request. – Matthias Bauch Mar 11 '14 at 20:47
  • URLs have a maximum length. You can't create arbitrarily long URLs and expect them to work. Different browsers support different lengths. Different web servers support different lengths. – rmaddy Mar 11 '14 at 20:48
  • @rmaddy I tried `[postString appendFormat:[NSString stringWithFormat:@"&%@=%@", StrackArray, trackArrayList]];` but got a warning: Format string is not a string literal (potentially insecure) and doesnt add if over 200 lines – Ernie Mar 11 '14 at 20:51
  • @Ernie Example: `[postString appendFormat:@"?%@=%@", Sfrom, from];` – rmaddy Mar 11 '14 at 20:52
  • 1
    You should really prepare you data to `POST` them to the server, instead putting them as `GET`-parameter in the url. This will work for a large set of data. – Sascha Manuel Hameister Mar 11 '14 at 20:56
  • @rmaddy nope, its still not sending the longer strings – Ernie Mar 11 '14 at 21:05
  • @SaschaHameister can you please explain more, the data in php is as `GET` e.g. `$name = $_GET["name"];` – Ernie Mar 11 '14 at 21:08
  • 2
    There's a maximum length of [2,000 characters](http://stackoverflow.com/a/417184/849645) for a GET (as in the maximum length of the URL) – Rich Mar 11 '14 at 21:16

2 Answers2

1

Ernie, if you prepare the data to use POST you can access the data on your PHP-server using $_POST['name']...

NSURL *url = [NSURL URLWithString:@"Your php scripts URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];


[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];        
-1

Got it working as it should with this

NSURL *url = [NSURL URLWithString:shareURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"%@=%@", Sfrom, from] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"&%@=%@", Sto, to] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"&%@=%@", Sname, name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"&%@=%@", Stype, type] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"&%@=%@", Sduration, duration] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"&%@=%@", StrackArray, trackArrayList] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
Ernie
  • 89
  • 1
  • 11