首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >URLConnection sendSynchronousRequest问题?

URLConnection sendSynchronousRequest问题?
EN

Stack Overflow用户
提问于 2011-09-16 02:17:26
回答 3查看 3.6K关注 0票数 1

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

代码语言:javascript
复制
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];

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

EN

回答 3

Stack Overflow用户

发布于 2013-03-02 14:28:46

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

代码语言:javascript
复制
NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
 [urlRequest setHTTPMethod:@"POST"];
 urLConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

下面的委托方法可以做到这一点

代码语言:javascript
复制
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    [receivedData setLength:0];
}

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

    [receivedData appendData:data];

}  

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

代码语言:javascript
复制
  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}

您最好从已完成的连接中获得响应,它告诉您所有数据都已收到

代码语言:javascript
复制
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection  
{  
recievedData //the complete data 
}
票数 0
EN

Stack Overflow用户

发布于 2015-05-14 14:38:38

尝尝这个

代码语言:javascript
复制
    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文档的更多信息

票数 0
EN

Stack Overflow用户

发布于 2015-08-13 14:37:10

代码语言:javascript
复制
//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循环:

代码语言:javascript
复制
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"]);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7435537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档