2013-02-18 66 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