appendString NSString stringWithFormat不传递值

时间:2014-03-11 20:38:41

标签: ios objective-c nsstring nsarray stringwithformat

我有一个通过php连接到数据库的应用程序,我试图将一些大数据发送到数据库,例如坐标列表

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

像这样的4行的字符串添加没有问题,150行的字符串也被添加但是在200行左右标记它没有,并且我试图用它发布的其余数据也会丢失。 我已经使用php测试了这个,通过创建.txt并将数据添加到它,如果它超过200行,它就没有到达php文件,但150再次没问题。

我取坐标并将它们放入数组中,

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];

数组被传递到另一个数组trackArrayList等,并在此方法中发布到php文件。

-(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];        
    }

}

我尝试在[postString appendString:[NSString stringWithFormat:@"&%@=%@", StrackArray, trackArrayList]];等不同版本中编写NSMutableString stringWithFormat,但结果相同。

字符串是不是足以容纳这么多行,或者是应用程序在发布之前内存不足。

对可能导致此问题或其他任何方式撰写appendString的任何建议。

2 个答案:

答案 0 :(得分:1)

Ernie,如果您准备使用POST数据,则可以使用PHP访问$_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 :(得分:-1)

使用此

使其正常工作
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];