邮寄与邮寄之间的区别在iOS的Json解析中获取方法 [英] Difference between Post & Get method in Json Parsing in ios

查看:119
本文介绍了邮寄与邮寄之间的区别在iOS的Json解析中获取方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了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& and之间有什么区别?获取方法? *

还有其他方法可以解析json吗?

解决方案

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

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

GET方法: GET方法通过将信息附加到URL(带有问号)并将其存储为Querystring集合来发送信息. 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-后得到之间的差异

I implement the JSON Parsing as follow:

-(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"];

}

Using this code, it slow down the app. How can I parse the json in background? *Is this right for Post Method? what is the difference between Post & Get Method?*

Is there any other way to json parsing?

解决方案

You are using synchronous request which is executed on Main thread so if you need to do it in background use asynchronous loading.

POST METHOD: The POST method generates a FORM collection, which is sent as a HTTP request body. All the values typed in the form will be stored in the FORM collection.

GET METHOD: The GET method sends information by appending it to the URL (with a question mark) and stored as A Querystring collection. The Querystring collection is passed to the server as name/value pair. The length of the URL should be less than 255 characters.

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.

Check this answer for more details : what-is-the-difference-between-post-and-get

这篇关于邮寄与邮寄之间的区别在iOS的Json解析中获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆