0

I am downloading the data from the web service API.In my project i have to download data using different APIs in so many places.So every where Iam calling web service, this leads to complexity of code.

    - (void)downloadInvitedVcards
{
    AppManager *oAppManager = [AppManager getSharedInstance];
    [oAppManager hasreachabilityChanged];
    if (oAppManager.currentConnectivity) {
        //NSString *deviceUDID = oAppManager.deviceID;

        NSArray *keys = [NSArray arrayWithObjects:@"UserName",@"PMobileNo",nil];
        NSArray *objects = [NSArray arrayWithObjects:oAppManager.userName,oAppManager.mobileno,nil];
        NSData *_jsonData = nil;
        NSString *_jsonString = nil;


        NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

        if ([NSJSONSerialization isValidJSONObject:jsonDictionary]) {
            _jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
            _jsonString = [[NSString alloc] initWithData:_jsonData encoding:NSUTF8StringEncoding];

        }

        NSString *newUrl = oAppManager.loginURL;
        NSString *appendUrl = [newUrl stringByAppendingString:@"/DownloadInvitedVcards"];

        NSURL *aUrl = [NSURL URLWithString:appendUrl];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:_jsonData];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:[NSString stringWithFormat:@"%d",[_jsonData length]] forHTTPHeaderField:@"Content-Length"];

        NSError *errorReturned = nil;
        NSURLResponse *theResponse = [[NSURLResponse alloc] init];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];

        if (errorReturned)
        {
            NSLog(@"Error %@",errorReturned);
        }
        else {
            NSString *responseString = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSLog(@"%@",responseString);

            NSArray *responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            for (NSDictionary *resultData in responseData) {
                NSLog(@"Value :%@",resultData);
            }
        }
    }

}

whererever required Iam doing the same process but changing the API method. It is working fine but this leads to more code and increasing complexity. Is there any better way to achieve this.

Sudhakar Tharigoppula
  • 2,857
  • 3
  • 16
  • 17
  • 1
    So what is your problem. Create a class file with public method(parameter) of this and call wherever you want. – Kumar KL Jan 22 '14 at 06:49

1 Answers1

0

It sounds like your problem is a design one more than a code one. I would look into creating a single class whose job is to simple manage requests to and from the server. It would wrap up all the mechanics of making requests, serialising results etc. Then the rest of your app would simply have to ask it to retrieve the data they want or forward their requests to the server.

drekka
  • 20,957
  • 14
  • 79
  • 135
  • I need to call a method with parameters JsonDictionary and ApIMethod which returns the response data NSDictionary *responseData = [[NSURLManager getSharedInstance] getResponseAPI:jsonDictionary andAPT:@"/DownloadQRCode"]; is this correct way of doing – Sudhakar Tharigoppula Jan 22 '14 at 09:16
  • There is no "correct" way of doing things in IT. Every programmer I know does something different. However there are ways of doing things which are easier to maintain or extend, etc. This is where design patterns come in, providing concepts which help you to write software that is more robust. The solution you mention is similar to how I might solve this problem. It wraps all the boiler plate code inside a method. Another thing I would suggest to look at is using KVC to populate classes with return data. – drekka Jan 22 '14 at 23:32