POST XML request to 'REST' service url from iPhone - xcode4

I m pretty much new to objective-C and strucked at a point.I have to POST XML request to rest service url and get the response from it .I m using NSMutableRequest.But couldn't see any response.Couldn't understand where I m going wrong ...
Request XML is like this:
<GetUserRequest xmlns="http://schemas.datacontract.org/2004/07/DModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ID>456789</ID>
<UserEmail>John#yahoo.com</UserEmail>
</GetUserRequest>
My url is like this : http://192.158.0.104:8732/IServices/GetXml
MY code goes like this :
NSString *urlString1=[NSString stringWithFormat:#"http://192.158.0.104:8732/IServices/GetXml"];
NSMutableURLRequest *request=[[[NSMutableURLRequest alloc] init]autorelease];
[request setURL:[NSURL URLWithString:urlString1]];
[request setHTTPMethod:#"POST"];
NSString *contentType=[NSString stringWithFormat:#"application/xml"];
[request addValue:contentType forHTTPHeaderField:#"Content-type"];
NSMutableData *postBody=[NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:#"<GetUserRequest xmlns=\"http://schemas.datacontract.org/2004/07/DModel\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"<ID>456789</ID>"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"<UserEmail>John#yahoo.com</UserEmail>"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"/GetUserRequest>"]dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
NSHTTPURLResponse *urlResponse=nil;
NSError *error=[[NSError alloc]init];
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *rss=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding ];
NSlog(#"Response Code:%d",[urlResponse statusCode]);
if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
{
NSLog(#"Response:%#",rss);
}
But I couldnot see anything in active console...
Any help would be appreciated ...

That code looks poor to me and doesn't contain enough error checking ([NSURLConnection sendSynchronousRequest] can return nil when an error occurs). Modify the end to:
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];
if (responseData != nil)
{
NSString *rss = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding ];
NSlog(#"Response Code:%d",[urlResponse statusCode]);
if([urlResponse statusCode ]>=200 && [urlResponse statusCode]<300)
{
NSLog(#"Response:%#",rss);
}
}
else
{
NSLog(#"Failed to send request: %#", [error localizedDescription]);
}

Related

Returning a NSMutableURLRequest with a url that contains quotes

NSString *urlString = #"http://example.com/myzip?\"30339\"";
NSLog(#"urlstring: %#", urlString);
// RETURNS=> urlstring: http://example.com/myzip?"30339"
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSLog(#"request: %#", request);
// RETURNS=> request: <NSMutableURLRequest: 0x8f54480> { URL: (null) }
I am unable to complete a NSMutableURLRequest if my URL has quotes inside of it. Is there a work around for this?
" is not a legal character for use in URLs, and must be escaped instead (as %22), like so:
http://example.com/myzip?%2230339%22

Core Data Fetch is Returning Nothing

In this app, I am using an sqlite database that was created by another app. I can query the database using the Firefox SQLite Manager and see that what I am searching for does exist in the database. I have reduced my query to something very simple, but still get nothing returned in my NSFetchedResultsController.
Here is my code:
- (NSFetchedResultsController*) frc {
if (!frc_) {
#autoreleasepool {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:#"INDEX" inManagedObjectContext:[ManagedObjectContext moc]];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:15];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"lemma = 'dog'"];
[NSFetchedResultsController deleteCacheWithName:nil];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:#"lemma" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController =
[[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:[ManagedObjectContext moc]
sectionNameKeyPath:#"lemma"
cacheName:nil];
aFetchedResultsController.delegate = (id<NSFetchedResultsControllerDelegate>)self;
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error]) {
NSLog(#"Unresolved Error %#, %#", error, [error userInfo]);
abort();
}
self.frc = aFetchedResultsController;
}
}
return frc_;
}
There is an entity called "INDEX" in the data model. To confirm that the entity has the lemma property, I show the content of its lemma property, getting this:
po [[entity propertiesByName] objectForKey:#"lemma"]
(id) $3 = 0x1104f740 (<NSAttributeDescription: 0x1104f740>), name lemma, isOptional 1, isTransient 0, entity INDEX, renamingIdentifier lemma, validation predicates (
), warnings (
), versionHashModifier (null)
userInfo {
}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)
Examining the contents of the aFetchedResultsController immediately after the fetch (where it is assigned to self.frc) gives this:
po aFetchedResultsController
(NSFetchedResultsController *) $4 = 0x1105c5c0 <NSFetchedResultsController: 0x1105c5c0>
po [[aFetchedResultsController fetchedObjects] count]
(id) $1 = 0x00000000 <nil>
I suppose the problem here is something very basic, but I don't know what I am overlooking.
I found the answer at the Ray Wenderlich site. The problem is that the sqlite file must be moved from the application bundle to the application documents directory.
This is done by copying it form the bundle into the documents directory if it is not already there.
Here is the code for creating the persistent store coordinator:
- (NSPersistentStoreCoordinator *)pstore {
if (pstore_ != nil) {
return pstore_;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"words.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath: storePath];
// THIS IS THE KEY PIECE TO IMPORTING AN EXISTING SQLITE FILE:
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle]
pathForResource:#"words" ofType:#"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
pstore_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.mom];
if(![pstore_ addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:options error:&error]) {
// Error for store creation should be handled in here
}
return pstore_;
}

AFNetworking Expected content type "application/xml", "text/xml", text/plain

I am trying to log into google reader using AfNetworking AfHttpClient but I am getting this error that I can;t seem to figure out.
Below is my subclass of AFNetworking:
// main url endpoints
#define GOOGLE_ACCOUNTS_BASE_URL #"https://www.google.com/accounts/"
#implementation ADPGoogleLoginClient
+ (ADPGoogleLoginClient *)sharedClient {
static ADPGoogleLoginClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[ADPGoogleLoginClient alloc] initWithBaseURL:[NSURL URLWithString:GOOGLE_ACCOUNTS_BASE_URL]];
});
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFXMLRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:#"Content-type" value:#"text/plain"];
[self setDefaultHeader:#"Accept" value:#"text/plain"];
return self;
}
#end
And then I try to form a request by using the following code:
//set up request params
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
#"googlereader-ios-client", #"client",
[keychainCredentials objectForKey:(__bridge id)kSecAttrAccount], #"Email",
[keychainCredentials objectForKey:(__bridge id)kSecValueData], #"Passwd",
#"reader", #"service",
#"ipad", #"source", nil];
//make requests
[[ADPGoogleLoginClient sharedClient] getPath:#"ClientLogin"
parameters:params
success:^(AFHTTPRequestOperation *operation , id responseObject)
{
//parse out token and store in keychain
NSString* responseString = [operation responseString];
NSString* authToken = [[[responseString componentsSeparatedByString:#"\n"] objectAtIndex:2]
stringByReplacingOccurrencesOfString:#"Auth=" withString:#""];
keychainToken = [[KeychainItemWrapper alloc] initWithIdentifier:#"GReaderToken" accessGroup:nil];
[keychainToken setObject:authToken forKey:(__bridge id)kSecValueData];
loginSuccess();
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"There was an error logging into Reader - %#", [error localizedDescription]);
loginFailure(error);
}];
Im setting the default headers to
[self setDefaultHeader:#"Content-type" value:#"text/plain"];
[self setDefaultHeader:#"Accept" value:#"text/plain"];
so im not sure why it still thinks it is expecting xml?
You are setting the Content-Type header for the request, but the error you're seeing is from the response. In order for requests to be considered "successful", the content type needs to match up with expectations (so as to avoid trying to parse a JSON response when you expected XML).
In that same error code, it should have mentioned what content type it actually got back. If it is indeed XML, add that using AFXMLRequestOperation +addAcceptableContentTypes:, and everything should work just fine.
Hi Matt so I just got around to testing this. Looks like I was registering the wrong AF operation. I changed
[self registerHTTPOperationClass:[AFXMLRequestOperation class]];
to
[self registerHTTPOperationClass:[AFHTTPRequestOperation class]];
and all was good!

Calling Webservice by NSURLConnection issues, Not called everytime?

I am calling .NET webservice hosted locally from my iPhone app (iOS5) like this:
(IBAction)btnCallService:(id)sender {
[XYZActivity startAnimating];
// So I could show activity on my main UI thread.
[self performSelector: #selector(CallXYZSService)
withObject: nil
afterDelay: 0];
}
(void) CallXYZSService
{
NSURL *url = [NSURL URLWithString: #"http://localwinhost/JSON_Service.asmx/GetFunction1"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:1 timeoutInterval:30];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"close" forHTTPHeaderField:#"Connection"];
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody: requestData];
//[req setHTTPBody: [postStr dataUsingEncoding:NSUTF8StringEncoding]];
myConn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (myConn) {
webData = [NSMutableData data];
}
else
{
NSLog(#"Connection Failed");
}
}
}
-(void) connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *) response{
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *)connection
didReceiveData:(NSData *) data {
[webData appendData:data];
}
-(void) connection:(NSURLConnection *)connection
didFailWithError:(NSError *) error {
NSLog(#"Service Failed");
}
Now my problem is sometimes it calls the service and I recive data , and sometimes it doesn'tand nothing happens.... not even timeout error...
What I am doing wrong here , I am calling my webservice asynch ... issue is it even doesn't go to any other delegate methods , just prepare the request , init the connection and then nothing happens....
Is it has something to do with previous connections to the webservice ? As for testing I am calling 2,3 different functions on same service on same server ... but these are happening on different viewcontrollers ...so I init new connection and set my connection to nil in connectionDidFinishLoading method.
Can any one plz help me here, how to make sure that I always get response back from service either valid response or some error.... or timeout....
If I test the same service on MAC in Safari , it always get called without any issue...!!!
Thanks,
Maverick
I have changed the code as below
I have changed the code as below NSString *jsonRequest_NE = [NSString stringWithFormat:#"ID=%#&Password=%#",strID,strPwd];
NSString *jsonRequest = [jsonRequest_NE stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"Request: %#", jsonRequest);
NSString *myURL = #"http://localwinhost/JSON_Service.asmx/GetFunction1";
NSString *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
but still the same beahviour, sometime it works sometimes it just doesn't.....
I have searched alot...but still could't find proper solution...
any one plz help me hereeeeeeeeeeee !!!
Make sure you don't have any non-UFT8 characters in your jsonRequest variable.
Let me know how you managed it.

RestKit: RKObjectManager's postObject:mapResponseWith:delegate

I want to send as an object as json
HTTP Headers: {
"Content-Length" = 24;
"Content-Type" = "application/json";
}
HTTP Body: {"userId":"5","mode":""}.
and get back a json
HTTP Headers: {
"Content-Type" = "application/json";
}
HTTP Body: {"insertCount":4,"offerCount":0,"favoriteOfferCount":0,"favoriteInsertCount":0}
and map it to an object, can anyone guide me as i'm getting many errors involving routers,object loader etc
I understand that i have to use RKObjectManager's postObject:mapResponseWith:delegate:
but configuring it is confusing.
never mind here's how i did it
#interface RestKitUtil : NSObject <RKObjectLoaderDelegate,RKRequestDelegate>
-(void)postobj;
#end
#implementation RestKitUtil
-(void)postobj{
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
[RKObjectManager objectManagerWithBaseURL:#"http://10.6.10.121:80/CW_war"];
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[CountResultVO class]];
[mapping mapKeyPathsToAttributes:
#"offercount", #"offercount",
#"insertCount", #"insertCount",
#"favouriteOfferCount", #"favouriteOfferCount",
#"favouriteInsertCount", #"favouriteInsertCount",
nil];
RKObjectRouter *router = [RKObjectManager sharedManager].router;
[router routeClass:[BasicUserArgVo class] toResourcePath:#"/getMyOffersCount" forMethod:RKRequestMethodPOST];
[RKObjectManager sharedManager].objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:#"objStore.sqlite"];
RKObjectMapping *listMapping = [RKObjectMapping mappingForClass:[BasicUserArgVo class]];
[listMapping mapKeyPath:#"userId" toAttribute:#"userId"];
[listMapping mapKeyPath:#"mode" toAttribute:#"mode"];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:listMapping ];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[listMapping inverseMapping] forClass:[BasicUserArgVo class]];
[[RKObjectManager sharedManager] setSerializationMIMEType:RKMIMETypeJSON];
//NSUTF8StringEncoding
[[RKObjectManager sharedManager] setAcceptMIMEType:RKMIMETypeJSON];
BasicUserArgVo *bvo = [[BasicUserArgVo alloc] initWithUserId:5];
[[RKObjectManager sharedManager] postObject:bvo mapResponseWith:mapping delegate:self];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
CountResultVO* cvo = [objects objectAtIndex:0];
NSLog(#"values:%ld,%ld,%ld,%ld",[cvo offercount],[cvo insertCount],
[cvo favouriteOfferCount],[cvo favouriteInsertCount] );
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
NSString *msg = [NSString stringWithFormat:#"Error: %#", [error localizedDescription]];
NSLog(#"log : %#",msg);
}
#end

Resources