2013-02-18 72 views
0
NSString *myString = @"1994"; 
    NSString *post =[[NSString alloc] initWithFormat:@"data=%@",myString]; 

    NSURL *url=[NSURL URLWithString:@"http://nyxmyx.com/Kinkey/KinkeyPHP/lastid2.php/?data=%@",myString]; 
    NSLog(@"URL%@",url); 

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSLog(@"postDATA%@",postData); 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    NSLog(@"postLENGTH%@",postLength); 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 

    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 

    NSError *error1 = [[NSError alloc] init]; 
    NSHTTPURLResponse *response = nil; 
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error1]; 
    NSString *string; 
    if ([response statusCode] >=200 && [response statusCode] <300) 
     { 
     string = [[NSString alloc] initWithData:urlData encoding:NSMacOSRomanStringEncoding]; 
     UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"alert1" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [alert1 show]; 
     } 

我是新的目标c。当我用参数发送NSURL时,它会给出错误,因为“太多的参数期望1有2”如何用一个参数更改我的url?NSURL传递一个参数

+1

当您传递时,您不必“alloc init NSError”它是参考。 'NSError * error = nil'是正确的。 – 2013-02-18 05:23:20

回答

1

我已经纠正了一些东西,测试你的代码,它现在应该是罚款:从上面的调用

NSString *myString = @"1994"; 
NSString *post =[[NSString alloc] initWithFormat:@"data=%@",myString]; 

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://nyxmyx.com/Kinkey/KinkeyPHP/lastid2.php/?data=%@",myString]]; 
NSLog(@"URL%@",url); 

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
NSLog(@"postDATA%@",postData); 
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
NSLog(@"postLENGTH%@",postLength); 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 

[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:postData]; 

NSError *error1 = [[NSError alloc] init]; 
NSHTTPURLResponse *response = nil; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error1]; 
NSString *string; 
if ([response statusCode] >=200 && [response statusCode] <300) 
    { 
    string = [[NSString alloc] initWithData:urlData encoding:NSMacOSRomanStringEncoding]; 
    UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"alert1" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
    [alert1 show]; 
    } 

响应:

1995,prabu,1231231233,antab,8080808080,1360738531881.jpg,编号
1996,prabu,1231231233,antab,8080808080,1361013972284.jpg,编号
1997,prabu,1231231233,antab,8080808080,1360844505212.jpg,编号
1998 ,josh,0417697070,null,+ 61420224346,1361160944442.jpg,编号
1999,josh,0417697070,null,+ 61420224346,1356047464383.jpg,编号
2000,josh,0417697070,null,+ 61420224346,1361160816141.jpg,编号
wooza,0420224346,J Wratt,+ 61417697070,2013-56-1803-56-17.jpg,否
2003,测试,9894698946,ggh hjj,9894598945,2013-11-1811-11-40.jpg,是

+0

感谢您的帮助。 – user2067403 2013-02-18 08:43:43

2

更换

NSURL *url=[NSURL URLWithString:@"http://nyxmyx.com/Kinkey/KinkeyPHP/lastid2.php/?data=%@",myString]; 

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://nyxmyx.com/Kinkey/KinkeyPHP/lastid2.php/?data=%@",myString]]; 
+0

由于无法识别的选择器在postLength发送到实例0x8f85860后出现错误 – user2067403 2013-02-18 05:11:56

+0

发布该错误的日志,我会看看我是否可以提供帮助。 – 2013-02-18 05:14:02

+0

对不起评论。它工作正常。感谢您的帮助 – user2067403 2013-02-18 06:04:40