你好朋友我在连接类中遇到问题我正在为我的不同方法使用相同的连接类。每个工作正常但是在使这个特定连接时我得到错误,即:..连接确实失败并出现错误。< / p>
我发布我的全部代码请帮帮我。 发送我的网址和建立连接的代码: -
NSString *strURl=[NSString stringWithFormat:@"http://xxxyyy.com/API/index.php?action=listIt&login=%@&pwd=%@&name=%@&catID=%d&listID=&addr1=%@&addr2=%@&locName=&city=%@&state=%@&zip=&startDate=%@&startHours=%@&startMin=%@&startMeridiem=%@&endDate=%@&endHours=%@&endMin=%@&endMeridiem=%@&contactName=&contactPhone=&contactEmail=&contactWebsite=&cost=%d&description=%@",appDelegate.strUserName,appDelegate.strPassword,strTitle,self.subCat_ID,[txtAddress1.text urlEncodeUsingEncoding:NSUTF8StringEncoding],[txtAdress2.text urlEncodeUsingEncoding:NSUTF8StringEncoding],lblCity.text,lblState.text,strBeginDate,startHr,startMin,startMeridiem,strEndDate,strEndHr,strEndMin,strEndMeridiem,cost,[txtdDscription.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
//strURl=[strURl urlEncodeUsingEncoding:NSUTF8StringEncoding];
// NSString *strEncode=[strURl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// strEncode=[strEncode stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@" strListItURl =%@",strURl);
ConnectionClass *connection=[[ConnectionClass alloc]init];
[connection getResponseData:strURl];
connection.delegate=self;
[connection release];
NSLog(@“strListItURl =%@”,strURl);这是我正在发送的方法和参数的网址。 strListItURl http://xxxyyy.com/API/index.php?action=listIt&login=sawanth&pwd=sawanth&name=Hello One&amp; catID = 4&amp; listID =&amp; addr1 = One%20tow&amp; addr2 = next%20one&amp; locName =&amp; city = Anchorage&amp; state = Alaska&amp; zip =&amp; startDate = 11/8 / 2012&安培; startHours = 11&安培; startMin = 33&安培; startMeridiem = AM&安培;结束日期= 11/10/2012&安培; endHours = 11&安培; endMin = 33&安培; endMeridiem = AM&安培; CONTACTNAME =安培; contactPhone =安培; contactEmail =安培; contactWebsite =安培;成本= 3及描述=%20Hello
注意: - 当我直接在浏览器上粘贴“strListItURl”时,我获得了成功。
现在连接后我进入连接类
-(void)getResponseData:(NSString*)strURL
{
NSLog(@"strURL =%@",strURL);
// NSString *str1 = [NSString stringWithFormat:@"%@",strURL];
NSURL *url=[NSURL URLWithString:strURL];
NSLog(@"url is =%@",strURL);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
//Initialize the connection for NSURL Connection
theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
if(theConnection)
{
//Initialize the data and retain the data
receiveData = [[NSMutableData data] retain];
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please check internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
这里当我打印urlRequest时,我正在寻找 打印urlRequest的描述:NSURLRequest(null)
最终的控制权去了 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
请告诉我我做错了什么:)
答案 0 :(得分:0)
你没有分配NSURLRequest。但奇怪的是打印null而不是nil。
尝试:
NSURLRequest *urlRequest = [[NSURLRequest alloc] requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
答案 1 :(得分:0)
我得到了答案。 实际上当我通过strURL
时NSString *strURl=[NSString stringWithFormat:@"http://xxxyyy.com/API/index.php?action=listIt&login=%@&pwd=%@&name=%@&catID=%d&listID=&addr1=%@&addr2=%@&locName=&city=%@&state=%@&zip=&startDate=%@&startHours=%@&startMin=%@&startMeridiem=%@&endDate=%@&endHours=%@&endMin=%@&endMeridiem=%@&contactName=&contactPhone=&contactEmail=&contactWebsite=&cost=%d&description=%@",appDelegate.strUserName,appDelegate.strPassword,strTitle,self.subCat_ID,[txtAddress1.text urlEncodeUsingEncoding:NSUTF8StringEncoding],[txtAdress2.text urlEncodeUsingEncoding:NSUTF8StringEncoding],lblCity.text,lblState.text,strBeginDate,startHr,startMin,startMeridiem,strEndDate,strEndHr,strEndMin,strEndMeridiem,cost,[txtdDscription.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
它从某个地方占用了空白区域,由于这个原因,我的连接因错误而失败。 所以我修剪了空格
strURl = [strURl stringByReplacingOccurrencesOfString:@" " withString:@""];
它的开始工作正常。