AFNetworking responseObject contains hex - how do I check for that?
I am developing an app and an API that the app will talk to. I am pretty new to the developing an app side of thing!
Although the API should always return valid JSON, I want to handle the case where something goes wrong and it returns HTML (think DNS screwup).
I am using AFNetworking
and self.sessionmanager
is an AFHTTPSessionManager
Right now, my (very simplified) code says :
[self.sessionmanager GET:@"/app/api/v1/meetings/" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject containsObject:@"status"]) {
// Do stuff
}
else {
// Do failure stuff
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// Do failure stuff
return;
}];
The problem is that if I arrive at the success function with an HTML page instead of JSON, I get a responseObject
that is hex and when I call containsObject
I get an NSInvalidArgumentException
with unrecognized selector sent to instance.
I tried putting a @try @catch block around the containsObject
call, but it didn't seem to catch the exception.
How can I check whether responseObject
is a valid object from a JSON response, in order to avoid the call to containsObject
when required?
ios xcode afnetworking
add a comment |
I am developing an app and an API that the app will talk to. I am pretty new to the developing an app side of thing!
Although the API should always return valid JSON, I want to handle the case where something goes wrong and it returns HTML (think DNS screwup).
I am using AFNetworking
and self.sessionmanager
is an AFHTTPSessionManager
Right now, my (very simplified) code says :
[self.sessionmanager GET:@"/app/api/v1/meetings/" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject containsObject:@"status"]) {
// Do stuff
}
else {
// Do failure stuff
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// Do failure stuff
return;
}];
The problem is that if I arrive at the success function with an HTML page instead of JSON, I get a responseObject
that is hex and when I call containsObject
I get an NSInvalidArgumentException
with unrecognized selector sent to instance.
I tried putting a @try @catch block around the containsObject
call, but it didn't seem to catch the exception.
How can I check whether responseObject
is a valid object from a JSON response, in order to avoid the call to containsObject
when required?
ios xcode afnetworking
Check ifresponseObject
is aNSArray
or aNSDictionary
, withif ([responseObject isKindOfClass[NSArray class]]){}, etc.
. But it's strange, because if I remember correctly and if you use aAFHTTPResponseSerializer
set toAFJSONResponseSerializer
you should have the guarantee that the response is always JSON and so be nil (with an error, calling the failure block?) in case that the response is HTML.
– Larme
Nov 15 '18 at 18:27
@Larme - that did it, thanks. Not sure how I'm getting the response through though!
– Ben Holness
Nov 15 '18 at 19:19
add a comment |
I am developing an app and an API that the app will talk to. I am pretty new to the developing an app side of thing!
Although the API should always return valid JSON, I want to handle the case where something goes wrong and it returns HTML (think DNS screwup).
I am using AFNetworking
and self.sessionmanager
is an AFHTTPSessionManager
Right now, my (very simplified) code says :
[self.sessionmanager GET:@"/app/api/v1/meetings/" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject containsObject:@"status"]) {
// Do stuff
}
else {
// Do failure stuff
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// Do failure stuff
return;
}];
The problem is that if I arrive at the success function with an HTML page instead of JSON, I get a responseObject
that is hex and when I call containsObject
I get an NSInvalidArgumentException
with unrecognized selector sent to instance.
I tried putting a @try @catch block around the containsObject
call, but it didn't seem to catch the exception.
How can I check whether responseObject
is a valid object from a JSON response, in order to avoid the call to containsObject
when required?
ios xcode afnetworking
I am developing an app and an API that the app will talk to. I am pretty new to the developing an app side of thing!
Although the API should always return valid JSON, I want to handle the case where something goes wrong and it returns HTML (think DNS screwup).
I am using AFNetworking
and self.sessionmanager
is an AFHTTPSessionManager
Right now, my (very simplified) code says :
[self.sessionmanager GET:@"/app/api/v1/meetings/" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject containsObject:@"status"]) {
// Do stuff
}
else {
// Do failure stuff
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// Do failure stuff
return;
}];
The problem is that if I arrive at the success function with an HTML page instead of JSON, I get a responseObject
that is hex and when I call containsObject
I get an NSInvalidArgumentException
with unrecognized selector sent to instance.
I tried putting a @try @catch block around the containsObject
call, but it didn't seem to catch the exception.
How can I check whether responseObject
is a valid object from a JSON response, in order to avoid the call to containsObject
when required?
ios xcode afnetworking
ios xcode afnetworking
asked Nov 15 '18 at 17:49
Ben HolnessBen Holness
1,01811733
1,01811733
Check ifresponseObject
is aNSArray
or aNSDictionary
, withif ([responseObject isKindOfClass[NSArray class]]){}, etc.
. But it's strange, because if I remember correctly and if you use aAFHTTPResponseSerializer
set toAFJSONResponseSerializer
you should have the guarantee that the response is always JSON and so be nil (with an error, calling the failure block?) in case that the response is HTML.
– Larme
Nov 15 '18 at 18:27
@Larme - that did it, thanks. Not sure how I'm getting the response through though!
– Ben Holness
Nov 15 '18 at 19:19
add a comment |
Check ifresponseObject
is aNSArray
or aNSDictionary
, withif ([responseObject isKindOfClass[NSArray class]]){}, etc.
. But it's strange, because if I remember correctly and if you use aAFHTTPResponseSerializer
set toAFJSONResponseSerializer
you should have the guarantee that the response is always JSON and so be nil (with an error, calling the failure block?) in case that the response is HTML.
– Larme
Nov 15 '18 at 18:27
@Larme - that did it, thanks. Not sure how I'm getting the response through though!
– Ben Holness
Nov 15 '18 at 19:19
Check if
responseObject
is a NSArray
or a NSDictionary
, with if ([responseObject isKindOfClass[NSArray class]]){}, etc.
. But it's strange, because if I remember correctly and if you use a AFHTTPResponseSerializer
set to AFJSONResponseSerializer
you should have the guarantee that the response is always JSON and so be nil (with an error, calling the failure block?) in case that the response is HTML.– Larme
Nov 15 '18 at 18:27
Check if
responseObject
is a NSArray
or a NSDictionary
, with if ([responseObject isKindOfClass[NSArray class]]){}, etc.
. But it's strange, because if I remember correctly and if you use a AFHTTPResponseSerializer
set to AFJSONResponseSerializer
you should have the guarantee that the response is always JSON and so be nil (with an error, calling the failure block?) in case that the response is HTML.– Larme
Nov 15 '18 at 18:27
@Larme - that did it, thanks. Not sure how I'm getting the response through though!
– Ben Holness
Nov 15 '18 at 19:19
@Larme - that did it, thanks. Not sure how I'm getting the response through though!
– Ben Holness
Nov 15 '18 at 19:19
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325253%2fafnetworking-responseobject-contains-hex-how-do-i-check-for-that%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325253%2fafnetworking-responseobject-contains-hex-how-do-i-check-for-that%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check if
responseObject
is aNSArray
or aNSDictionary
, withif ([responseObject isKindOfClass[NSArray class]]){}, etc.
. But it's strange, because if I remember correctly and if you use aAFHTTPResponseSerializer
set toAFJSONResponseSerializer
you should have the guarantee that the response is always JSON and so be nil (with an error, calling the failure block?) in case that the response is HTML.– Larme
Nov 15 '18 at 18:27
@Larme - that did it, thanks. Not sure how I'm getting the response through though!
– Ben Holness
Nov 15 '18 at 19:19