URL解码ios中的json [英] URL Decoding of json in ios
问题描述
我在php中有1个网络服务,浏览器网址为:
http:Domain / webservice / ACTEC_WebService。 php?lastupdate = 2012-09-01 01:00:00
现在当我尝试获取url在我的iphone应用程式,它采取这个网址:
http://Domain/webservice/ACTEC_WebService.php?lastupdate = 2012 -09-01%2001:00:00
这是造成问题。
$ b SBJSON * json = [
$ b 我已使用此代码从我的网址提取数据。 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:@URLNameofType:@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 / jsonforHTTPHeaderField:@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
消息:@检查您的网络连接委托:self cancelButtonTitle:@OK
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
else
{
......
}
这里,我得到null响应url正在解码...我如何使这个解决...
帮助我。
提前感谢。
NSString * urlLoc = [[fileContents objectForKey :@URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableData * postData = [NSMutableData data];
NSMutableURLRequest * urlRequest;
[postData appendData:[[NSString stringWithFormat:@添加您要发送的数据] 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-urlencodedforHTTPHeaderField:@Content-Type];
[urlRequest setHTTPBody:postData];
NSString * temp = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
NSLog(@\\\
\\\
url =%@ \\\
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);
也许这会帮助你。
I am having 1 webservice in php which is having browser url mentioned bel :
http:Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01 01:00:00
Now when I try to fetch url in my iphone app, it is taking this url:
http://Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01%2001:00:00
This is creating problem.
i have used this code for fetching data from my 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
{
......
}
here,i am getting null response as url is getting decoded...How can I make this solved... Help me out. Thanks in advance.
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);
check nslog. that which data u send to service.
NSData *response = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"json_string >> %@",json_string);
Maybe this will help you.
这篇关于URL解码ios中的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!