我正在尝试使用下面的代码来“触发”网址。 Web服务器不返回任何数据。但NSURLConnection正在建立。
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);
}
答案 0 :(得分:0)
当你写:
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest
delegate:self];
您正在启动异步网址连接。
然后,您将立即测试连接是否成功,并使用 local 范围创建NSMutableData
的实例。您的NSURLConnectionDelegate
方法(您尚未发布)无法访问此本地范围的NSMutableData
变量。
您是否确实实施了NSURLConnectionDelegate
协议的方法?
答案 1 :(得分:0)
尝试发送同步请求以本地化问题:
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest: theRequest
returningResponse: nil
error: &error];
NSLog(@"error = %@, \ndata = %@", error, returnData);
答案 2 :(得分: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;)
我没有使用这么多,所以如果不自己完全阅读文档,就无法进一步扩展;这是你需要做的。 : - )