无法将项目ID填充到任务URL中

时间:2014-03-03 07:53:55

标签: ios loading uipickerview nsurl

我的Picker视图项目,任务和子任务中有三个组件。我的要求是选择我想要分别显示项目ID的项目以及与子任务类似的项目。我得到了项目ID,我不知道如何将此项目ID附加到任务URL以加载相应的任务。这是我的代码

NSArray *projectNames;
NSArray *taskNames;
NSArray *subtaskNames;
NSArray * projID;
NSArray * taskID;
NSArray * subtskID;
@synthesize lblProjects;
@synthesize lblTasks;
@synthesize lblSubTasks;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        lblProjects.text =  [[NSString alloc] init];
    }
    return self;
}
- (id)init
{
    // since we don't wanna re-implement allocation and instantiation for every
    // initializer, we call the 'designated initializer' with some default values,
    // in this case the default nibName and bundle are nil.
    return [self initWithNibName:nil bundle:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadprojects];
    [self loadtasks];
    [self loadsubtasks];


}
    -(void)loadprojects
    {
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.projectpicker dataSource]];
        //NSString *pickername = [self.projectpicker dataSource];
        NSString *key = @"Da9s^a2Rp4na6R$ikiAsav3Is@niWsa";

        //NSString *encrypteduname = [AESCrypt encrypt:pickername password:key];

        // Code for Project loading
        NSString *projecturl = @"http://exampleId.com/GetAssignedProjects/313";

        NSURL *url = [NSURL URLWithString:projecturl];

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];




        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];


        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                                    cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                timeoutInterval:30];


        // Make synchronous request
        urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                        returningResponse:&response
                                                    error:&error];
        if ([response statusCode] >= 200 && [response statusCode] < 300)
        {
            NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData
                                                                     options:NSJSONReadingAllowFragments error:&error];

            NSArray *entries = [NSJSONSerialization JSONObjectWithData:[responseData dataUsingEncoding:NSUTF8StringEncoding]
                                                               options:0 error:&error];
            if(!entries)
            {
                NSLog(@"Error : %@", error);
            }
            else{

                for (NSDictionary *entry in entries) {
                    projID = [entries valueForKey:@"ID_PROJECT"];
                    projectNames = [entries valueForKey:@"NM_PROJECT"];
                }
                NSLog(@"projID : %@", projID);
                _projectpicker.delegate = self;
                _projectpicker.dataSource = self;
            }

        }       else {

        }
    }
    -(void)loadtasks
    {

        // Code for Tasks loading
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.projectpicker dataSource]];
        NSString *nsTaskurllocal = @"http://exampleId.com/GetAssignedTasks/";
        NSString *usrid = @"313";
        NSString * productIdString =[NSString stringWithFormat:@"%@/%@",[lblProjects.text self],usrid];
        NSLog(@"aString : %@", productIdString);
        NSString *aString = [nsTaskurllocal stringByAppendingString:productIdString];


        NSURL *nstaskurl = [NSURL URLWithString:aString];
        NSLog(@"nstaskurl : %@", nstaskurl);

        NSData *nstaskpostData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *nstaskpostLength = [NSString stringWithFormat:@"%lu", (unsigned long)[nstaskpostData length]];
        NSMutableURLRequest *nstaskrequest = [[NSMutableURLRequest alloc] init];
        [nstaskrequest setURL:nstaskurl];
        [nstaskrequest setHTTPMethod:@"POST"];
        [nstaskrequest setValue:nstaskpostLength forHTTPHeaderField:@"Content-Length"];
        [nstaskrequest setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"];
        [nstaskrequest setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"];
        [nstaskrequest setHTTPBody:nstaskpostData];


        NSError *nstaskerror = [[NSError alloc] init];
        NSHTTPURLResponse *nstaskresponse = nil;
        NSData *nstaskurlData=[NSURLConnection sendSynchronousRequest:nstaskrequest returningResponse:&nstaskresponse error:&nstaskerror];
        NSURLRequest *nstaskurlRequest = [NSURLRequest requestWithURL:nstaskurl
                                                          cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                      timeoutInterval:30];


        // Make synchronous request
        nstaskurlData = [NSURLConnection sendSynchronousRequest:nstaskurlRequest
                                              returningResponse:&nstaskresponse
                                                          error:&nstaskerror];
        if ([nstaskresponse statusCode] >= 200 && [nstaskresponse statusCode] < 300)
        {
            NSString *nstaskresponseData = [NSJSONSerialization JSONObjectWithData:nstaskurlData
                                                                           options:NSJSONReadingAllowFragments error:&nstaskerror];

            NSArray *nstaskentries = [NSJSONSerialization JSONObjectWithData:[nstaskresponseData dataUsingEncoding:NSUTF8StringEncoding]
                                                                     options:0 error:&nstaskerror];
            if(!nstaskentries)
            {
                NSLog(@"Error : %@", nstaskerror);
            }
            else{

                for (NSDictionary *nstaskentry in nstaskentries) {
                    taskID = [nstaskentries valueForKey:@"ID_TASK"];
                    taskNames = [nstaskentries valueForKey:@"TASk_NAME"];

                    //NSLog(@"Error : %@", taskNames); //log to see the result in console // by Kiran
                }

                _projectpicker.delegate = self;
                _projectpicker.dataSource = self;
            }

        }       else {

        }
    }
    -(void)loadsubtasks
    {
        // Code for Sub Tasks loading
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.projectpicker dataSource]];
        NSString *nsSubTaskurl = @"http://exampleId.com/GetAssignedSubTasks/164/313";

        NSURL *nssubtaskurl = [NSURL URLWithString:nsSubTaskurl];

        NSData *nssubtaskpostData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *nssubtaskpostLength = [NSString stringWithFormat:@"%lu", (unsigned long)[nssubtaskpostData length]];
        NSMutableURLRequest *nssubtaskrequest = [[NSMutableURLRequest alloc] init];
        [nssubtaskrequest setURL:nssubtaskurl];
        [nssubtaskrequest setHTTPMethod:@"POST"];
        [nssubtaskrequest setValue:nssubtaskpostLength forHTTPHeaderField:@"Content-Length"];
        [nssubtaskrequest setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"];
        [nssubtaskrequest setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"];
        [nssubtaskrequest setHTTPBody:nssubtaskpostData];


        NSError *nssubtaskerror = [[NSError alloc] init];
        NSHTTPURLResponse *nssubtaskresponse = nil;
        NSData *nssubtaskurlData=[NSURLConnection sendSynchronousRequest:nssubtaskrequest returningResponse:&nssubtaskresponse error:&nssubtaskerror];
        NSURLRequest *nssubtaskurlRequest = [NSURLRequest requestWithURL:nssubtaskurl
                                                             cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                         timeoutInterval:30];


        // Make synchronous request
        nssubtaskurlData = [NSURLConnection sendSynchronousRequest:nssubtaskurlRequest
                                                 returningResponse:&nssubtaskresponse
                                                             error:&nssubtaskerror];
        if ([nssubtaskresponse statusCode] >= 200 && [nssubtaskresponse statusCode] < 300)
        {
            NSString *nssubtaskresponseData = [NSJSONSerialization JSONObjectWithData:nssubtaskurlData
                                                                              options:NSJSONReadingAllowFragments error:&nssubtaskerror];

            NSArray *nssubtaskentries = [NSJSONSerialization JSONObjectWithData:[nssubtaskresponseData dataUsingEncoding:NSUTF8StringEncoding]
                                                                        options:0 error:&nssubtaskerror];
            if(!nssubtaskentries)
            {
                NSLog(@"Error : %@", nssubtaskerror);
            }
            else{

                for (NSDictionary *nssubtaskentry in nssubtaskentries) {
                    subtaskNames = [nssubtaskentries valueForKey:@"SUBTASK_NAME"];

                    //NSLog(@"Error : %@", subtaskNames);
                }
                _projectpicker.delegate = self;
                _projectpicker.dataSource = self;
            }

        }       else {

        }
    }

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    int numberofRows;
    switch (component) {

        case 0:
            numberofRows = [projectNames count];
            break;
        case 1:
            numberofRows = [taskNames count];
            break;
        case 2:
            numberofRows = [subtaskNames count];
            break;
    }
    return numberofRows;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title;
    switch (component) {
        case 0:
            title = [projectNames objectAtIndex:row];
            break;

        case 1:
            title = [taskNames objectAtIndex:row];
            break;
        case 2:
            title = [subtaskNames objectAtIndex:row];
            break;
    }
    return title;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    NSNumber *myProjectArrayString = [projID objectAtIndex:row];
    //NSNumber *myTaskArrayString = [taskID objectAtIndex:row];
    //NSLog(@"%@",myArrayString);
    //NSLog(@"%@",myTaskArrayString);
    lblProjects.text = [NSString stringWithFormat:@"%@",myProjectArrayString];
    //lblProjects.hidden = YES;
    lblTasks.text = [taskNames objectAtIndex:[pickerView selectedRowInComponent:1]];
    //lblTasks.text = [NSString stringWithFormat:@"%@", myTaskArrayString];
    lblSubTasks.text = [subtaskNames objectAtIndex:[pickerView selectedRowInComponent:2]];
}

简单说话我想在项目ID的帮助下加载任务URL

先谢谢 基兰库马尔

0 个答案:

没有答案