get responseObject on failure block AFNetworking 3.0
Just do this in your failure block:-
NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"%@",errResponse);
For Swift:-
var errResponse: String = String(data: (error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] as! NSData), encoding: NSUTF8StringEncoding)
NSLog("%@", errResponse)
Updated for Swift 4.1
var errResponse: String = String(data: (error._userInfo![AFNetworkingOperationFailingURLResponseDataErrorKey] as! Data), encoding: String.Encoding.utf8)!
print(errResponse)
AFNetworking 3.0 status code
Hope this helps!!!
typedef enum RequestTypes{
GET,
POST,
PUT,
DELETE
}RequestType;
@property (nonatomic, strong) AFHTTPSessionManager *manager;
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; // Base URL here, not entire URL
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
//Add Headers to request if any. // header is a dictionary of key value pair.
for (NSString *key in [headers allKeys]) {
[self.manager.requestSerializer setValue:[headers valueForKey:key] forHTTPHeaderField:key];
}
/* Serialize request by type */ /* Type can be GET, POST etc */
NSString *requestTypeMethod = [self getStringForRequestType:type];
/* enter your URL here excluding base url */
NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:requestTypeMethod URLString:[[NSURL URLWithString:urlString relativeToURL:_manager.baseURL] absoluteString] parameters:params error:nil];
NSURLSessionDataTask *dataTask = [self.manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
// here you will get actual response, NSURL response as well and error if any.
}];
[dataTask resume];
#pragma mark - GET Request type as string
-(NSString *)getStringForRequestType:(RequestType)type {
NSString *requestTypeString;
switch (type) {
case GET:
requestTypeString = @"GET";
break;
case POST:
requestTypeString = @"POST";
break;
case PUT:
requestTypeString = @"PUT";
break;
case DELETE:
requestTypeString = @"DELETE";
break;
default:
requestTypeString = @"GET";
break;
}
return requestTypeString;
}
AFNetworking: how to get the response string when failed?
You may log operation.responseString
line in success and failure block both to get response string of server.
If webservice has some kind of error and provide invalid json or response then log [error description]
in failure block.
For reference please check below code snippet.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:120];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:[[NSString stringWithFormat:@"%@",URL]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Response: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@ \n %@", operation.responseString,[error description]);
}];
AFNetworking 3.0 GET request save into class in iOS
Add all events to an array, you will then have an array of dictionaries. Also make a new array which will hold your new instances.
Now:
for (NSDictionary *dictionary in myArrayContainingAllEvents){
GeeksLocation *location = [GeeksLocation alloc] initWithDictionary:dictionary];
[newArrayForInstances addObject: location];
}
In the GeeksLocation class add the following initializer:
-(instanceType)initWithDictionary: (NSDictionary *) dictionary{
self = [super init];
if(self){
self.geeksAddress = [dictionary objectForKey:address];
self..... = [dictionary objectForKey:.......];
}
return self;
}
Hope this helps
Related Topics
Submitting iOS App to App Store Application Identifier Invalid
How to Get 18-Digit Current Timestamp in Swift
Uploads Using Backgroundsessionconfiguration and Nsurlsessionuploadtask Cause App to Crash
How to Call Https Url in Uiwebview
How to Get All Available Wifi Network Name Listings in iOS
Launchd_Sim Crashing: Could Not Create Temporary State Directory
Submitting iOS App Using Beta Version of Xcode
Ibm Worklight 6.1 - Failed Re-Generating the iPhone Native Folder
How to Prevent a Remote Notification from Being Displayed
What Is the Swift Preprocessor Equivalent to iOS Version Check Comparison
How to Open a Link to a PDF with Wkwebview
Firebase - How to Get the Key Value in Observeeventtype = Value