Reputation: 3803
I wrote two code both are same
My NSURLConnection
code works fine but AFNetworking
code don’t. Problem is I am getting JSON response from my NSURLConnection
code but I am not getting JSON
response from AFNetworking
code but I am getting response code as 200. . I have tried all possible combination but still no luck. I dont know what is the problem. I wnat to use AFNetworking
going forward so please someone help me. Thank you, -- Amit
NSURLConnection:
- (IBAction)connectHandler:(UIButton *)sender {
//This is where we attempt to connect to the URL represented by self.labelHostURL
//along with exception handling
NSString *request_type = @"STARTUP";
NSString *request_data = @"{\"lstCacheRefDt\":\"10/10/2010\",\"langCd\":\"en_US\",\"timeZn\":\"America/Chicago\",\"brndId\":\"WM\",\"prgmId\":\"WM0012\"}";
NSLog(@"Request: %@", request_data);
NSURL *url = [NSURL URLWithString:[ self.labelHostURL text ]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//NSData *requestData = [NSData dataWithBytes:[request_data UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSString* request_body = [NSString
stringWithFormat:@"request_type=%@&request_data=%@",
[request_type stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[request_data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection connectionWithRequest:request delegate:self]; }
AFNetworking:
- (void) getHomeData {
NSURL *url = [[NSURL alloc] initWithString:@"https://localhost:8443/MNMMLMockService/mAccountsWeb/services/mnapp/rpc"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *homePostParams1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"10/10/2010", @"lstCacheRefDt",
@"en_US", @"langCd",
@"America/Chicago", @"timeZn",
@"WM", @"brndId",
@"WM0012", @"prgmId", nil];
NSDictionary *homePostParams = [NSDictionary dictionaryWithObjectsAndKeys:@"STARTUP", @"request_type", homePostParams1,@"request_data", nil];
NSLog(@"Dic: %@", homePostParams);
NSMutableURLRequest *homeRequest = [httpClient requestWithMethod:@"POST" path:@"" parameters:homePostParams];
[homeRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[homeRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:homeRequest
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"JSON %@", JSON);
NSLog(@"operation StatusCode: %d", [response statusCode]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start]; }
Upvotes: 0
Views: 6006
Reputation: 411
I use AFNetworking library but you seem to be missing some steps that I do. I usually subclass AFHTTPClient. Whether you subclass or not, make sure to call AFHTTPClient's registerHTTPOperationClass method.
For example in my AFHTTPClient subclass during the init process, I do (where self is AFHTTPClient instance) :
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
This will set the appropriate accept headers for you.
For operations (all expect json responses), I do something like:
AFHTTPRequestOperation *httpOperation = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseJSONObject) { // some logic}
failure:^(AFHTTPRequestOperation *operation, NSError *error) { // some logic }];
Upvotes: 2
Reputation: 3006
I do not understand why you want to send url form encoded data instead of JSON to the server, but expect JSON back. Don't set the header fields. Remove this:
[homeRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[homeRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
If you want to set that (though you shouldn't have to) you do it with AFHTTPClient
. For example:
[httpClient setParameterEncoding:AFJSONParameterEncoding];
Furthermore, you do know that AFNetworking
encodes/decodes JSON for you, right? You pass it an object and it will automatically encode it as JSON and likewise will automatically take JSON received and decode it to an objective-c object. So you will never receive plain JSON from AFNetworking
library, you will receive the corresponding object.
Edit
I see your problem now. You are expecting AFNetworking
to url form encode half of the parameters, but also JSON encode the other part. With AFNetworking
you must either encode the entire parameter object as one thing or the other. I suggest you change to 100% JSON by keeping the homePostParams you created and encoding it with JSON, then rewriting the server to expect all JSON.
If you must have it split like that, you can't rely on AFNetworking
to do the encoding/decoding for you. You must do what you did with the NSURLConnection
:
NSString *request_data = @"{\"lstCacheRefDt\":\"10/10/2010\",\"langCd\":\"en_US\",\"timeZn\":\"America/Chicago\",\"brndId\":\"WM\",\"prgmId\":\"WM0012\"}";
NSDictionary *homePostParams = [NSDictionary dictionaryWithObjectsAndKeys:@"STARTUP", @"request_type", request_data,@"request_data", nil];
Kind of defeats the purpose of AFNetworking
though. Would be much better if the server just expected to receive JSON and not url form encoding
Upvotes: 2