I am making a synchronous call to the web service and sometimes I get the correct result back from the web service and sometimes I get HTML result indicating a Runtime error. Is there anything on the iOS side I have to do to correctly call the web service. Here is my code:
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 allo开发者_JS百科c] initWithData:data
encoding:NSUTF8StringEncoding];
Is it because I am not releasing properly?
you have to set the delegate methods of urlconnection like this
NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
urLConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
and the following delegate methods do the trick
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
[receivedData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
you will receive error in the following delegate if connection fails
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}
you better get the response from the finished connection which tells that all the data been received
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
recievedData //the complete data
}
try this
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 ");
this method works for me for more information read facebook documents for ios login
//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"]);
}
精彩评论