1

这让我很困惑。如果我输入以下内容:

NSString *LoginURLString = [NSString stringWithFormat:@"http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=13242134"];
//NSLog output:  http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084

并在 URL 请求中使用它,它可以正常工作,但我需要使其动态化,因此我使用以下命令将 URL 字符串与新的 UserID 连接起来:

NSString *user = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults]stringForKey:@"CustomerID"]];

//user = [[NSUserDefaults standardUserDefaults] stringForKey:@"CustomerID"];

NSString *LoginURLString = [NSString stringWithFormat:@"http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=%@", user];
//NSLog output:  http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084    

这是我的请求初始化程序的其余部分:

 NSString *urlString = LoginURLString;
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

以及处理请求的其他方法:

   -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [responseData setLength:0];
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [responseData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection{
    //   [connection release];

    CommonPickUpArray = [[NSMutableArray alloc] init];
    CommonLocationInfoArray = [[NSMutableArray alloc] init];

    NSString *data = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
    NSLog(@"%@", data);
}

这个请求甚至永远不会开始。我真的不明白为什么。我试图将这两个字符串输出到 NSLog 并且无论哪种方式它们看起来都完全相同。谁能解释一下?谢谢你的帮助!

编辑: ConnectiondidFailWithError方法正在输出:

错误域=NSURLErrorDomain 代码=-1000“错误 URL”UserInfo=0xf6a6f50 {NSUnderlyingError=0xf6a75d0“错误 URL”,NSLocalizedDescription=错误 URL}

答案 1 的输出:

2012-05-08 13:45:24.959 AmericanTaxi[1295:707] 连接失败并出现错误:错误的 URL 2012-05-08 13:45:24.960 AmericanTaxi[1295:707] 的 URL:(null)

urlString 和 LoginURLString 的输出:

2012-05-08 13:57:40.415 AmericanTaxi[1320:707] LoginURLString: http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084

2012-05-08 13:57:40.417 AmericanTaxi[1320:707] urlstring: http://dispatch.americantaxi.com:8080/AT/servlet/OnlineOrderServices?command=retrieveCustomerCommonPlaces&customerId=2314084

4

1 回答 1

1

在您的 didFailWithError 中,检查 URL 并通过将其添加到您的 didFailWithError 委托来查看它为什么不好:

NSLog(@"Connection failed with error: %@", [error localizedDescription]);
NSLog(@"for the URL: %@", [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

发布结果。

于 2012-05-08T17:37:49.503 回答