使用NSURLConnection WebService

时间:2011-06-13 13:11:59

标签: iphone objective-c xcode

当我运行下面给出的程序

时,我得到以下错误

错误: Error Domain = NSURLErrorDomain Code = -1000“bad URL”UserInfo = 0x5948b80 {NSUnderlyingError = 0x5948ac0“bad URL”,NSLocalizedDescription = bad URL}

我需要做plz建议我

谢谢你..

代码如下:

@implementation WebSampleViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    dataWebService = [[NSMutableData data] retain];


    NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@" http://www.googleapis.com/customsearch"]] retain];


    NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];

    [myConnection start];    

    [super viewDidLoad];
}



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{

    [dataWebService setLength:0];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [dataWebService appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{

    NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];

    NSLog(@"Response: %@",responseString);

    [responseString release];

    [dataWebService release];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{

    NSLog(@"Error during connection: %@", [error description]);
}

3 个答案:

答案 0 :(得分:0)

在没有URL字符串开头的空格的情况下尝试。

答案 1 :(得分:0)

您不应该retain 请求 NSMutableURLRequest类型的实例。

您的字符串网址中有一个前导空格。

使用以下代码。

NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.googleapis.com/customsearch"]];

我认为,您应该检查您的网址,我提供与iPhone上相同的“未找到”。

答案 2 :(得分:0)

您必须删除网址中的空格