gpt4 book ai didi

ios - ios中Json解析中Post和Get方法的区别

转载 作者:行者123 更新时间:2023-11-28 19:08:42 28 4
gpt4 key购买 nike

我按如下方式实现 JSON 解析:

-(void)getallEvent
{
SBJSON *json = [SBJSON new];
json.humanReadable = YES;
responseData = [[NSMutableData data] retain];

NSString *service = @"/GetAllVenue";

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

//NSLog(@"request string:%@",requestString);

// NSString *requestString = [NSString stringWithFormat:@"{\"GetAllEventsDetails\":\"%@\"}",service];
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];

// self.connection = [NSURLConnection connectionWithRequest:request delegate:self];



NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];

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

}
else
{
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

NSDictionary *results = [[responseString JSONValue] retain];
//NSLog(@" %@",results);
NSString *extractUsers = [[results objectForKey:@"d"] retain];
NSDictionary *finalResult = [[extractUsers JSONValue] retain];
NSLog(@"Final Results : %@",finalResult);
listOfEvents = [finalResult objectForKey:@"List of Event details of given Venue"];

使用此代码会降低应用程序的速度。如何在后台解析 json?*这适用于 Post 方法吗? Post 和 Get 方法有什么区别?*

有没有其他的json解析方式?

最佳答案

您正在使用在主线程上执行的同步请求,因此如果您需要在后台执行此操作,请使用异步加载。

发布方法: POST 方法生成一个 FORM 集合,它作为 HTTP 请求正文发送。表单中键入的所有值都将存储在 FORM 集合中。

GET METHOD: GET 方法通过将信息附加到 URL(带有问号)并存储为查询字符串集合来发送信息。 Querystring 集合作为名称/值对传递到服务器。 URL 的长度应少于 255 个字符。

An HTTP GET is a request from the client to the server, asking for a resource.

An HTTP POST is an upload of data (form information, image data, whatever) from the client to the server.

查看此答案了解更多详情:what-is-the-difference-between-post-and-get

关于ios - ios中Json解析中Post和Get方法的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17854340/

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