2014-05-04 42 views
0

我想使用下面的代码'触发'的网址。 Web服务器不返回任何数据。但NSURLConnection正在建立中。NSUrlRequest不工作​​目标C

NSString *serverAddressTest = @"http://domain.com"; 
NSString *fullWebAddress = [NSString stringWithFormat:@"%@?CustomerName=%@&ContactNo=%@&Products=%@",serverAddressTest,customer,contactnumber,allProductsInString]; 

NSURL *url = [NSURL URLWithString:fullWebAddress]; 
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    NSMutableData *webData = [NSMutableData data]; 
    NSLog(@"%@",webData); 
} 
else { 
    NSMutableData *webData = [NSMutableData data]; 
    NSLog(@"%@",webData); 
} 
+3

很可能你应该对url进行编码 – Andrea

+1

错误委托方法是否被调用? – Wain

+0

如果(连接){NSMutableData * webData = [NSMutableData data];调试失败,则调试中将不会发生错误。 NSLog(@“%@”,webData); } – user3579247

回答

0

当你写:

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest 
                   delegate:self]; 

你开始一个异步URL连接。

然后紧接着,您正在测试连接是否成功,并创建NSMutableData本地作用域的实例。您的NSURLConnectionDelegate方法(您尚未发布)将无法访问此本地范围的变量NSMutableData

你确实实施了NSURLConnectionDelegate协议的方法吗?

0

尝试发送同步请求本地化问题:

NSError *error; 
NSData *returnData = [NSURLConnection sendSynchronousRequest: theRequest 
              returningResponse: nil 
                 error: &error]; 

NSLog(@"error = %@, \ndata = %@", error, returnData); 
0

你也还需要实现委托协议。 (由于NSBum说)

使用这里的苹果显示example 是在这些部分被放在一起:

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 

    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
receivedData = [NSMutableData dataWithCapacity: 0]; 

    // create the connection with the request 
    // and start loading the data 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if (!theConnection) { 
     // Release the receivedData object. 
     receivedData = nil; 
      NSLog(@"FAIL "); 
     // Inform the user that the connection failed. 
    } 



} 

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

    NSLog(@" response %@", response); 

    // This method is called when the server has determined that it 
    // has enough information to create the NSURLResponse object. 

    // It can be called multiple times, for example in the case of a 
    // redirect, so each time we reset the data. 

    // receivedData is an instance variable declared elsewhere. 
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // Append the new data to receivedData. 
    // receivedData is an instance variable declared elsewhere. 
    [receivedData appendData:data]; 

    NSLog(@" receivedData %@", receivedData); 
} 

receivedData不是本地的,但其他地方声明返回的数据。 (NSMutableData * receivedData;)

我不使用这么多,所以不能在没有完全读取文档的情况下进一步扩展;这是你需要做的。 :-)