2014-05-04 77 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;)

我不使用這麼多,所以不能在沒有完全讀取文檔的情況下進一步擴展;這是你需要做的。 :-)