我正在对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];
是因为我没有正确发布吗?
答案 0 :(得分:0)
你必须像这样设置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
}
答案 1 :(得分:0)
试试这个
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文档
答案 2 :(得分:0)
//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 loop:
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"]);
}