程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> xcode-NSURL帶有一個參數傳遞

xcode-NSURL帶有一個參數傳遞

編輯:編程綜合問答
NSURL帶有一個參數傳遞
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];
        }

用上面代碼傳遞帶有一個參數的NSURL時就會報錯:

Too many arguments expects 1 have 2

不知道該怎麼解決?謝謝

最佳回答:


我改了一下你的代碼,應該沒問題了:

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,No
1996,prabu,1231231233,antab,8080808080,1361013972284.jpg,No
1997,prabu,1231231233,antab,8080808080,1360844505212.jpg,No
1998,josh,0417697070,null,+61420224346,1361160944442.jpg,No
1999,josh,0417697070,null,+61420224346,1356047464383.jpg,No
2000,josh,0417697070,null,+61420224346,1361160816141.jpg,No
2001,wooza,0420224346,J Wratt ,+61417697070,2013-55-1803-55-54.jpg,No
2002,wooza,0420224346,J Wratt ,+61417697070,2013-56-1803-56-17.jpg,No
2003,testing,9894698946,ggh hjj,9894598945,2013-11-1811-11-40.jpg,Yes

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved