gpt4 book ai didi

iphone - ios中json的URL解码

转载 作者:行者123 更新时间:2023-11-29 04:23:02 25 4
gpt4 key购买 nike

我在 php 中有 1 个 Web 服务,其中提到了浏览器 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

这会产生问题。

我使用此代码从我的网址获取数据。

     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 被解码时,我收到空响应...我怎样才能解决这个问题...帮帮我。提前致谢。

最佳答案

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

也许这对你有帮助。

关于iphone - ios中json的URL解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12721885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com