2013-09-01 103 views
0

因此,我已收集了这些代码发送POST请求发送到Web服务,但是我不断收到此错误问题与HTTP POST请求的iOS

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString' 

,我已经看了看这是什么意思,但不明白本代码中的回应。

任何帮助非常感谢,谢谢

-(IBAction)buttonPressed:(id)sender{ 

NSString *myRequestString = @"idvar=76"; 

NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ]; 
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"192.168.0.11/test/deleteerrand.php" ] ]; 
[ request setHTTPMethod: @"POST" ]; 
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
[ request setHTTPBody: myRequestData ]; 
NSURLResponse *response; 
NSError *err; 
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err]; 
NSString *content = [NSString stringWithUTF8String:[returnData bytes]]; 
NSLog(@"responseData: %@", content); 

}

回答

0

试试这个

NSString *content = [[NSString alloc] initWithData: returnData encoding:NSASCIIStringEncoding]; 
+0

非常感谢!没有错误信息,但现在我有问题,似乎没有发生什么大声笑!时间来看看我的PHP:P 只是想知道,你能详细说明为什么这有效吗? –

+0

你传递了​​无效的参数,[NSString stringWithUTF8String接收字符串不是字节。 NSString类已经有了initWithData –