Ios URLConnection sendSynchronousRequest问题?

Ios URLConnection sendSynchronousRequest问题?,ios,web-services,Ios,Web Services,我对web服务进行同步调用,有时从web服务返回正确的结果,有时得到HTML结果,指示运行时错误。在iOS端,我需要做些什么才能正确调用web服务。这是我的密码: NSURLResponse *response = nil; NSError *error = nil; NSString *requestString = @"some parameters!"; NSMutableURLRequest *request = [[NSMutableURLReque

我对web服务进行同步调用,有时从web服务返回正确的结果,有时得到HTML结果,指示运行时错误。在iOS端,我需要做些什么才能正确调用web服务。这是我的密码:

NSURLResponse *response = nil; 
NSError *error = nil;         
NSString *requestString = @"some parameters!";        
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];        
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[requestString dataUsingEncoding:NSUTF8StringEncoding]];

NSData *data = [NSURLConnection sendSynchronousRequest:request 
                                     returningResponse:&response 
                                                 error:&error];    
NSString *responseData = [[NSString alloc] initWithData:data 
                                               encoding:NSUTF8StringEncoding];

是因为我没有正确释放吗?

您必须像这样设置urlconnection的委托方法

NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
 [urlRequest setHTTPMethod:@"POST"];
 urLConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
下面的委托方法可以做到这一点

    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    [receivedData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
{

    [receivedData appendData:data];

}  
如果连接失败,您将在以下委托中收到错误

  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}
您最好从完成的连接中得到响应,它告诉您所有数据都已收到

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection  
{  
recievedData //the complete data 
}
试试这个

    NSError * error;
NSURLResponse * urlresponse;

NSURL * posturl=[NSURL URLWithString:@"Type your webService URL here"];

NSMutableURLRequest * request=[[NSMutableURLRequest alloc]initWithURL:posturl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:50];

[request setHTTPMethod:@"POST"];
[request addValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

NSString * body=[NSString stringWithFormat:@"fbid=%@",userid];
[request setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

NSData * data=[NSURLConnection  sendSynchronousRequest:request returningResponse:&urlresponse error:&error];

if (data==nil) {
    return;
}

id jsonResponse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@" json response %@", jsonResponse);
if (![[jsonResponse objectForKey:@"code"] isEqualToNumber:[NSNumber numberWithInt:200]]) {
    NSLog( @" successFull ");
此方法适用于我,有关更多信息,请阅读ios登录的facebook文档

//set request    
NSURLRequest *req=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://indianbloodbank.com/api/donors/?bloodgroup=O%2B"]];

NSLog(@"Request-%@",req);

NSError *err=nil;
NSURLResponse *res=nil;

NSData *xmldata=[NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&err];

NSLog(@"Error-%@",err);
NSLog(@"Response-%@",res);
NSLog(@"XmlData-%@",xmldata);

xmldictionary=[XMLReader dictionaryForXMLData:xmldata error:&err];

NSLog(@"XmlDictionary-%@",xmldictionary);

mArray=[xmldictionary retrieveForPath:@"response.donorslist.donors"];

NSLog(@"MutableArray-%@",mArray);

lblname.text=[[mArray objectAtIndex:0]valueForKey:@"name"];
lbllocation.text=[[mArray objectAtIndex:0]valueForKey:@"location"];
lblphone.text=[[mArray objectAtIndex:0]valueForKey:@"phone"];

NSLog(@"%@,%@,%@",lblname.text,lbllocation.text,lblphone.text);
NSLog(@"%@",mArray);
For循环:

for (int i=0; i<mArray.count; i++)
{
    Data * don=[NSEntityDescription insertNewObjectForEntityForName:@"Data" inManagedObjectContext:app.managedObjectContext];

    don.donorid=[[mArray objectAtIndex:i]valueForKey:@"id"];
    don.gender=[[mArray objectAtIndex:i]valueForKey:@"gender"];
    don.name=[[mArray objectAtIndex:i]valueForKey:@"name"];
    don.location=[[mArray objectAtIndex:i]valueForKey:@"location"];
    don.phone=[[mArray objectAtIndex:i]valueForKey:@"phone"];

    [app saveContext];

    NSLog(@"%@,%@,%@,%@,%@",[[mArray objectAtIndex:i]valueForKey:@"id"],[[mArray objectAtIndex:i]valueForKey:@"gender"],[[mArray objectAtIndex:i]valueForKey:@"name"],[[mArray objectAtIndex:i]valueForKey:@"location"],[[mArray objectAtIndex:i]valueForKey:@"phone"]);

}

用于(int i=0;ii如果这是服务器上的运行时错误,并且不是由您的错误请求引起的,我认为应用程序中没有任何东西可以修复它。当您说您
获取HTML结果指示运行时错误时,应用程序是否崩溃,或者您的意思是存在服务器端错误?就像Flyingdiver所说的,没有什么可以修复的如果服务器端出现故障,则可以执行此操作。我应该说,除非错误是由发送错误数据引起的,否则应用程序无法执行任何操作。例如,调用webservice,addNumber并将其传递给“Z”和“T”进行添加。由于输入无效,服务器可能会返回错误。谢谢!奇怪的是,有时web服务会返回正确的数据结果,有时返回运行时错误。您好,Arun。添加一些关于应该确切更改的内容的详细信息。还编写一些描述性文本。