【问题标题】:How to print xml file returned by the server如何打印服务器返回的xml文件
【发布时间】:2013-11-04 14:43:09
【问题描述】:

这是我第一次通过 HTTP 请求处理服务器通信,为了确定我在做什么,并且有“我的双手之间的事情”,我想打印服务器应该的 xml 文件还给我。 如果有人可以帮助我,请:

这是我的代码:

(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

if ([stories count] == 0) {
 NSString * l_api_key = @"********************************************";
    NSString * l_secret_key = @"******************************************";

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[@myURLHERE stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
                                                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                        timeoutInterval:60.0];

    [theRequest setHTTPMethod:@"POST"];
    [theRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
    [theRequest setValue:@"application/x-www-form-unrlencoded" forHTTPHeaderField: @"Content-Type"];
    [theRequest addValue:l_api_key forHTTPHeaderField: @"EMApikey"];
    [theRequest addValue:[self hmacsha1:l_api_key secret:l_secret_key] forHTTPHeaderField: @"EMRequestHash"];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    NSLog(@"%@", theConnection);

    if( theConnection )
    {
        NSData *webData = [[NSMutableData data] retain];
        NSLog(@"%@", webData);
        //[self parseXMLFileAtURL:path];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

    [theConnection release];
}

cellSize = CGSizeMake([newsTable bounds].size.width, 60);

}

【问题讨论】:

    标签: objective-c xml parsing


    【解决方案1】:
    [NSURLConnection sendAsynchronousRequest:theRequest
                               queue:[NSOperationQueue mainQueue]
                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        NSLog(@"%@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
    }];
    

    比这样方便:

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    

    【讨论】:

    • 感谢你今天两次帮助我 Cy-4AH 你很善良 :) !
    【解决方案2】:

    您需要发送 POST 请求并取回响应,请参见下面的示例:

    //create the body
    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    //post
    [theRequest setHTTPBody:postBody];
    
    //get response
    NSHTTPURLResponse* urlResponse = nil;  
    NSError *error = [[NSError alloc] init];  
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];  
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response Code: %d", [urlResponse statusCode]);
    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
        NSLog(@"Response: %@", result);
    
                //here you get the response
    
    }
    

    【讨论】:

    • 非常感谢 aleroot 我会深入了解您的答案并尝试一下:D。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多