2012-10-04 83 views
0

我有其中有瀏覽器的URL中提到貝爾PHP 1個Web服務:URL解碼

http:Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01 01:00:00 

現在,當我嘗試在我的iPhone應用程序來獲取URL,它正在這個網址:

http://Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01%2001:00:00 

這是造成的問題。

我已經使用此代碼從我的網址獲取數據。

 SBJSON *json = [SBJSON new]; 
     json.humanReadable = YES; 
     responseData = [[NSMutableData data] retain]; 

     NSString *service = @""; 
     NSString *str; 
     str = @"LastUpdated"; 


     NSString *requestString = [NSString stringWithFormat:@"{\"LastUpdated\":\"%@\"}",str]; 

     // [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"WRONGANSWER"]; 

     NSLog(@"request string:%@",requestString); 
     NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; 


     NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"]; 
     NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc]; 
     NSString *urlLoc = [fileContents objectForKey:@"URL"]; 
     //urlLoc = [urlLoc stringByAppendingString:service]; 
     NSLog(@"URL : %@",urlLoc); 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: 
             [NSURL URLWithString:urlLoc]]; 
     NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; 
     [request setHTTPMethod: @"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:requestData]; 

     NSError *respError = nil; 
     NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ]; 
     NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; 
     NSLog(@"Resp : %@",responseString); 

     if (respError) 
     { 
      //  NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@", 
      //       [respError localizedDescription], 
      //       [[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]; 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ACTEC" 
                   message:@"check your network connection" delegate:self cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
      [alertView show]; 
      [alertView release]; 

     } 
     else 
     { 
...... 
} 

在這裏,我得到的URL空響應越來越解碼...我怎樣才能使這個解決...... 幫我個忙。 在此先感謝。

+0

試試這個NSString * urlLoc = [[fileContents objectForKey:@「URL」] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; –

+0

非常感謝幫助..但我只想收到此網址。我想收到此網址:http://173.73.144.114/ACTEC/webservice/ACTEC_WebService.php?lastupdate = 2012-09-01 01:00 :00因爲這2012-09-01 01:00:00 ..在我的休息代碼我比較這個字符串..所以我想在這種格式... –

+0

你有沒有得到迴應?首先告訴你,如果你有迴應,那麼問題是什麼。 –

回答

1
NSString *urlLoc = [[fileContents objectForKey:@"URL"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSMutableData *postData = [NSMutableData data]; 
    NSMutableURLRequest *urlRequest;   
    [postData appendData: [[NSString stringWithFormat: @"add your data which you want to send"] dataUsingEncoding: NSUTF8StringEncoding]]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
    urlRequest = [[NSMutableURLRequest alloc] init]; 
    [urlRequest setURL:[NSURL URLWithString:urlLoc]]; 
    [urlRequest setHTTPMethod:@"POST"]; 
    [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [urlRequest setHTTPBody:postData]; 

    NSString *temp = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding]; 
    NSLog(@"\n\nurl =%@ \n posted data =>%@",urlLoc, temp); 

檢查NSLog的。你發送哪些數據來服務。

NSData *response = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil]; 
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
NSLog(@"json_string >> %@",json_string); 

也許這會幫助你。

0

嘿親愛的只是一個又一個例子,我剛剛發佈我的這個代碼..

首先創建新SBJSON解析器對象

SBJSON *parser = [[SBJSON alloc] init]; 


NSURLRequest *request = [NSURLRequest requestWithURL: 
    [NSURL URLWithString:@"http:Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01%2001:00:00"]]; 

執行請求,並得到JSON回爲一個NSData對象

NSData *response = [NSURLConnection sendSynchronousRequest:request 
    returningResponse:nil error:nil]; 

從NSData響應獲取JSON作爲NSString

NSString *json_string = [[NSString alloc] 
    initWithData:response encoding:NSUTF8StringEncoding]; 

解析JSON響應爲對象這裏,我們使用NSArray的,因爲我們解析的JSON狀態對象的數組

NSArray *statuses = [parser objectWithString:json_string error:nil] 

而且在狀態數組你有你需要的所有數據。

我希望這會幫助你...

:)

+0

該數組給我空值 –

+0

字符串json_string也爲空 –

+0

哦在這裏在url一個空間,所以如果可能的話,然後刪除這個空間,然後再試一次... –