AFXMLRequestOperation with Google Docs - ios

I've tried several different approaches to pull content from a google doc with AFNetworking.
I've modified the acceptable content types.
Used AFKiss functionality
Used regular AFHttp operation
Still can't get any content from the following piece of code. Should be reproducible if anyone has some thoughts.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://spreadsheets.google.com/feeds/worksheets/0AhNgJb3GRiT3dEFkYmJVQ1pUUXBpdWlOYjFBMUtpOEE/private/full"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
NSLog(#"Finished");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParse) {
NSLog(#"Failed");
}];
[operation start];

View that link in a browser without being logged in to a Google account. It doesn't have any content, so you won't be able to load xml regardless of how you try do so.
You'll need to add Google authentication in your app to view the spreadsheet it seems. Information can be found in the Authenticating Users in Mobile Apps guide.

Related

AFNetworking setImageWithURLRequest not working

I am unable to download the images, The setImageWithURLRequest method is not going inside the success/failure block. Please someone advise me how solve this task. This is my code.
__weak UIImageView *images ;
NSString *url =[NSString stringWithFormat:#"%#%#",imageBaseUrl,eachImage];
NSLog(#"image download url %#",url);
[images setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if(response){
[arr addObject:image];
NSLog(#"Success fetching image");
}else{
NSLog(#"The image data is not there");
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Request failed with error: %#", error);
}];
I have checked above code and it is worked for other image URL. While i am used your image URL it is failed to load image.
if you add manually http:// to URL. Then image is load properly.
Your URL should be like http://3.0.191.16/uploads/patient/2133/38049.jpg
Your logic should be like below.
if url doesn't contain http then append http:// manually otherwise go with url from server.
Or you can ask API developer to send proper image URL.
Please mark as accept if it will work for you.

Using data only after fetching JSON information

I'm setting in a ViewController a NSString property by fetching a JSON table and then in a different ViewController I want to get that same property.
What is happening is when I'm trying to get the property this is nil.
I know what is the problem, I'm accessing the property in the main thread while the JSON fetching is still in progress in another thread.
I'm using the AFNETWORKING 2.0 framework to access the JSON table.
How can I wait for the property set and then use it?
I really appreciate any help you can provide.
You can do this in different ways, you can post notification from AFnetworkingJSON operation success callback like this. And observer that notification where you want to access that property. You can also pass a completionHandler to the method which can be call from success or failure callbacks.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"link"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// Post notification from here
// call completion handler if you have any
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response,
}
];
[operation start];

YouTube API v3 Error: 403 but the API key is configured and Bundle identifier is correct

I need to pull the name of a specific channels playlists from youtube. I am using AFJSONRequestOperation to make the call.
I keep getting the error code "Expected status code in (200-299), got 403". I have registered with google and my bundle identifier is correct.
Here is My code (I crossed out the last ten characters of my api KEY):
NSURL *url = [[NSURL alloc] initWithString:#"https://www.googleapis.com/youtube/v3/playlists?part=snippet&channelId=UCHvTZKuI5zQnxmbgSE-CFug&maxResults=50&key=AIzaSyDHU-hMgwLf0a4vZe4pPDxqyxxxxxxxxxx"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSLog(#"%#,",JSON[0][#"title"]);
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
NSLog( #"NSError: %#", error.localizedDescription);
}];
[operation start];
The solutions I have tried are as follows:
- Using the browser key.
- Regenerating the key.
- Triple checking the bundle identifier.
- have added the google cloud api's and the youtube analytics and the youtube data api.
Any Suggestions?
Thanks in advance,
-Joel
This seems to be a bug - I fixed it by removing the bundle identifier from my registered app.

Using AFNetworking _convertJSONString keeps allocated

I'm using AFNetworking, and I encountered a problem when I was calling a POST with JSON. I'm uploading several images in base64, and I noticed that even if I uploaded everything, _convertJSONString, or something related, is still in memory. Should be the JSON conversion applied by AFNetworking when I created the NSURLRequest, that actually should be released. I don't know if I'm missing something, but it's a weird behavior.
This is an example of how I'm implementing the request inside my client:
NSMutableURLRequest *request = [self requestWithMethod:#"POST" path:path parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
completionBlock(JSON, response, nil);
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
completionBlock(nil, response, error);
}];
[self enqueueHTTPRequestOperation:operation];
And this is the line where Instrument says the allocation comes from:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wassign-enum"
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]];
#pragma clang diagnostic pop
that's part of:
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
in AFHTTPClient.m
Thank you in advance for any help, or solution.
FIRST WORKAROUND
I've found that the NSURLRequest's content inside AFURLConnectionOperation is not completely released when the operation is finished, and this causes a leak.
Setting self.request = nil inside - (void)finish method solves the problem.
This is just a workaround, but I cannot currently find another way.
This is not an issue. Objects may be relatively long-lived in memory, without being a leak.
Don't worry about it.

AFNetworking+UIImageView placeholder image shows up, but not URL image

I'm trying to use the AFNetworking UIImageView call to load images from a URL as shown below:
[self.image setImageWithURL:[NSURL URLWithString:feed.imageURL] placeholderImage: [UIImage imageNamed:#"logo"]];
The placeholder image always shows up, but the actual image from "feed.imageURL" never does. I've verified that the URL is actually correct. I even hardcoded it to make sure, and still nothing.
My basic app setup is a tab controller...and in viewDidLoad, I call a method "fetchFeed" which performs the HTTP request to gather my JSON data.
My request block looks like:
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self parseDictionary:JSON];
isLoading = NO;
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Error: %#", error);
[self showNetworkError];
isLoading = NO;
[self.tableView reloadData];
}];
operation.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript", #"text/html", nil];
[queue addOperation:operation];
Turns out the server I was requesting the image from was sending content-type "image/jpg" and by default AFNetworking does not support this file type.
I changed the class method in AFImageRequestOperation to look like:
+ (NSSet *)defaultAcceptableContentTypes {
return [NSSet setWithObjects:#"image/tiff", #"image/jpeg", #"image/gif", #"image/png", #"image/ico", #"image/x-icon" #"image/bmp", #"image/x-bmp", #"image/x-xbitmap", #"image/x-win-bitmap", #"image/jpg", nil];
}
and it fixed my problem.
You can manage to accept what content-type you want with this library simply changing the request like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:yourURL];
[request addValue:#"image/*" forHTTPHeaderField:#"Accept"];
And call the AFNetworking method:
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
This way you will be able to override the content-type without changing the library.
AFNetworking doesn't support image/jpg MIME TYPE by default.
You can support it without modifying the AFNetworking Library
[AFImageRequestOperation addAcceptableContentType:#"image/jpg"];
All operations that manipulate the UI must be performed on the main thread. So you may need to use 'performSelectorOnMainThread:' when reloading your tableview data in the completion block.
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]
I had a similar problem but it turned out that I was passing a URL which contained spaces in it. When I properly encoded the URL using stringByAddingPercentEscapesUsingEncoding: the images now load.

Resources