0

我在 php 中有 1 个 web 服务,其中提到了 bel 浏览器 url:

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

现在,当我尝试在我的 iphone 应用程序中获取 url 时,它正在使用这个 url:

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

这正在制造问题。

我已使用此代码从我的 url 中获取数据。

     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 正在被解码......我怎样才能解决这个问题......帮帮我。提前致谢。

4

2 回答 2

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);

也许这会对你有所帮助。

于 2012-10-04T07:49:27.800 回答
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]

在状态数组中,您拥有所需的所有数据。

我希望这可以帮助你...

:)

于 2012-10-04T07:09:28.177 回答