Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top