2012-10-04 50 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一个空间,所以如果可能的话,然后删除这个空间,然后再试一次... –