Compiler warnings using AFNetworking - iphone

I am a newbie to AFNetworking and running a simple application to understand how AFNetworking is working. I am following this tutorial on MobileTuts:
I am already included the AFNetworking library and also typed -fno-objc-arc into the Compile Sources for each AFNetworking file.
I included this portion into the implementation file:
NSURL *url = [[NSURL alloc] initWithString:#"http://itunes.apple.com/search?term=harry&country=us&entity=movie"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"JSON");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failed with Error: %#, %#", error, error.userInfo);
}];
[operation start];
When I hit run, I have 14 semantic issues and the project failed.
For example one error is:
Method possibly missing a [super dealloc] call.
Not sure how to solve this error. Need some guidance...

Try this:
Firstly delete AFNetworking library and clean project.
Now download latest AFNetworking.
Add again AFNetworking library and build the project.

Related

How to determine reason for failure with AFNetworking AFJSONRequestOperation

I've inherited a project and am implementing AFNetworking and reading the docs it sounds great and much simpler than the current code. I have a url with json data so I'm doing the following, but getting the failure block and no idea why. I'm sure it's there, but how can I dig into AF and log the responses to determine failure reason. I know the url works, but perhaps its hitting the url but having trouble parsing?
NSString *listURL = [NSString stringWithFormat:GET_LIST,BASE_URL];
NSURL *url = [NSURL URLWithString:briefListURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.list = [NSArray arrayWithArray:(NSArray *)JSON];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
[self listOperationDidFail];
}];
thanks to #Larme this worked:
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(#"Error: %#", error); }
also, to answer my own issue above with text/plain. just add this before setting up operation:
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:#"text/plain"]];

AFNetworking Expected content type error

I am getting the json string in failure block
NSURL *url = [[NSURL alloc] initWithString:#"http://www.vinipost.com/Services/Update/UpdateService.asmx/GetPropSubType?"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:#"text/html"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"%#", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failed with Error: %#, %#", error, error.userInfo);
}];
[operation start];
Output:
Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
"text/json",
"text/javascript",
"application/json",
"text/html"
)}, got text/plain" UserInfo=0x71521a0 {NSLocalizedRecoverySuggestion=[{"PropTypId":1,"PropCatId":1,"PropTyp":"Flat/ Condo"}.......**
You need to add this line before operation
[AFJSONRequestOperation addAcceptableContentTypes:
[NSSet setWithObject:#"text/plain"]];
The error is clear:
the web service is returning a wrong content type.
The content type should be one of these:
"text/json",
"text/javascript",
"application/json",
"text/html"
But it returns
text/plain
Moreover, if you look at the http response, it returns HTML TAGS inside it, so AFNetworking is not able to parse.
If this page:
http://www.vinipost.com/Services/Update/UpdateService.asmx/GetPropSubType?
is under your control, correct the behavior removing html tags and changing the content type
In AFNetworking, you have to create NSURLRequest with the help of AFHTTPClient(So first you have to create AFHTTPClient and have to set some properties for this object) like below
AFHTTPClient *httpClient = [[httpClient alloc] initWithBaseURL:[NSURL URLWithString:#"http://www.vinipost.com/"]];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:#"Accept" value:#"application/json"];
httpClient.parameterEncoding = AFJSONParameterEncoding;
now if depends of GET/POST or any other type request you need to set parameter I consider it as POST Request, so set the Parameter dict and set all required Key Value pairs properly.if no parameters required you can pass Parameters as nil
NSURLRequest *request = [httpClient requestWithMethod:#"POST" path:#"Services/Update/UpdateService.asmx/GetPropSubType?" parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSLog(#"%#",JSON);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(#"Error MSG = %#",error);
}];
[operation start];
hope this will work for you :)

How to get AFHTTPRequestOperation to call selectors, not blocks, when completed?

AFHTTPRequestOperation likes to call GCD blocks when the request operation has been completed. Is there a way to get it to call method selectors instead? I am transitioning my app from ASIHTTPRequest to AFNetworking, and my app is built around selectors and not blocks.
You could call your selector inside of the completion block:
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:urlRequest
success:^(NSURLRequest *completedURLRequest, NSHTTPURLResponse *response, NSDictionary *json) {
[self callMyCustomSuccessMethod:json];
}
failure:^(NSURLRequest *errorRequest, NSHTTPURLResponse *response, NSError *error, id JSON) {
[self callMyCustomErrorMethod:error];
}];
[operation start];
Not sure if AFHTTPRequestOperation supports selector-based callbacks, but you can easily wrap a call to your selector in a block:
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[myDelegate onSuccess:operation];
}
This would work for a callback method declared as:
- (void)onSuccess:(AFHTTPRequestOperation*)operation;

Getting user's information after OAuth login

I want to get user's info. After OAuth I send this request:
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/account/verify_credentials.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Public Timeline: %#", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"error = %#", error);
NSLog(#"json = %#", JSON);}];
[operation start];
But got an error: {"error":"Could not authenticate you.","request":"\/1\/account\/verify_credentials.json"} Why I can't get access to information even after login in?
You haven't attached the OAuth token to the request. You're just sending a request with that URL string.
You might to have a look AFOAuth2Client which provides a neat extension to AFNetworking for doing authorized requests on Twitter.
Alternatively, you can use the built in iOS 5+ Twitter API, I wrote an answer for another SO post that shows you how exactly to do this - and it's super easy :)
If you need to implement OAuth from scratch, please see: https://github.com/bengottlieb/Twitter-OAuth-iPhone/
Good luck!

Empty JSON returned by AFNetworking on iOS5

I'm trying to load a simple JSON inside my iPhone app using the AFNetworking library. I'm using two different approaches:
NSURL *url = [NSURL URLWithString:#"http://www.mysite.com/test.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Name: %#", [JSON valueForKeyPath:#"name"]);
} failure:^(NSURLRequest* req,NSHTTPURLResponse *req2, NSError *error,id mex) {
NSLog(#"%#", [error description]);
}];
[operation start];
and using AFHttpClient (which would be my favorite,since it has all the features for dealing with my REST API:
NSString *baseurl = #"http://www.mysite.com";
NSString *path = #"/test.json";
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
//[client setAuthorizationHeaderWithUsername:#"myusername" password:#"mypassword"];
[client getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
//NSLog(#"sjson: %#", [JSON valueForKeyPath:#"entries"]);
NSLog(#"sjson: %#", JSON);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error Code: %i - %#",[error code], [error localizedDescription]);
}];
now,the problem: in both cases I'm receiving an empty Json. I've tested the server side and the web-service is giving the right response and the right MIME type (application/json).
I've already checked this discussion https://github.com/AFNetworking/AFNetworking/issues/175
but my target is pointing to 5.0 and ARC is not enabled for the AFNetworking library.
Maybe I'm missing something obvious here,but I cannot fix it at the moment.
Thanks a lot!
Claus