2014-02-27 73 views
0

從Picker視圖中選擇數據時,我得到了我的項目ID:12。現在我的問題是我想將這12(項目ID)發送到NSURL,以便在更改項目時分別加載我的任務。這裏是我下面的代碼:將Pickerview數據發送到URL

viewDidLoad中:

NSString *nsTaskurllocal = @"http://xxxx/xxx/"; 
    NSString *projectsLabel = [self.lblProjects text]; 
    NSString *aString = [nsTaskurllocal stringByAppendingString:projectsLabel]; 
    NSLog(@"aString : %@", aString); 
    NSString *temp = [aString stringByAppendingString:@"/"]; 

    int usrid = 313; 
    NSString *usridstring = [NSString stringWithFormat:@"%d", usrid]; 
    NSString *finalurl = [usridstring stringByAppendingString:temp]; 
    NSURL *nstaskurl = [NSURL URLWithString:finalurl]; 
    NSLog(@"nstaskurl : %@", nstaskurl); 

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

    NSString *nstaskpostLength = [NSString stringWithFormat:@"%lu", (unsigned long)[nstaskpostData length]]; 
    NSMutableURLRequest *nstaskrequest = [[NSMutableURLRequest alloc] init]; 
    [request setURL:nstaskurl]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:nstaskpostLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"]; 
    [request 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)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]]; 
} 

lblProjects.text有項目編號:12,所以我需要這個12傳遞給我的ASTRING,這樣我可以追加和它傳遞給NSURL Thnaks

+0

你想在服務機身右側發送信息(12)? –

+0

即12是動態ID。在這裏,用戶從選擇器視圖中選擇一個數據,並根據選擇的ID加載到標籤中,並且需要發送到NSURL,以便它可以分別加載任務項目選擇.. –

+0

你能告訴我你的最終結果網址看起來像。我想http:// xxxx/xxx/userID/projectId ...請讓我知道 –

回答

0

嘗試除以組件分割字符串,等等[splitArray lastObject]是你的 「12」

NSString *nsTaskurllocal = @"http://xxxx/xxx/"; 
NSString *projectsLabel = @"Project ID :12"; 
NSArray *splitArray = [projectsLabel componentsSeparatedByString:@":"]; 
NSString *aString = [NSString stringWithFormat:@"%@%@", nsTaskurllocal, [splitArray lastObject]]; 

ASTRING應該是現在。

0

試試這個,我希望這會有所幫助,如果你得到任何問題讓我知道

-(void)postMethod 
{ 
NSString *urlString = [NSString stringWithFormat:@"http:******"]; 
NSString *[email protected]"131"; 
NSString *[email protected]"12"; 
NSString * productIdString =[NSString stringWithFormat:@"%@/%@",userIDString,productIDString]; 
NSData* postData= [productIdString dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *decode = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",decode); 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 
NSString *boundary = @"---------------------------14737809831466499882746641449"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:postData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString*s11= [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
} 
+0

謝謝查蘭的快速反應,當試着用上面的代碼我得到 NSString * productIdString = [NSString stringWithFormat:@「%@ /%@」,[self.lblProjects text],usrid]; NSLog(@「aString:%@」,productIdString); http:// xxxx/xxxx/Label/313 –

+0

它爲我工作嘗試productId靜態檢查我的答案一次..現在我們正在解碼字符串,以檢查我們是否正確發送或不正確。檢查它並讓我知道 –