【问题标题】:NSURLConnection does trigger didReceiveData, but the returned data is zeroNSURLConnection 确实触发了didReceiveData,但是返回的数据为零
【发布时间】:2012-07-02 13:46:08
【问题描述】:

其他问题: 现在接收到的总数据长度为 89973。但是使用下面的代码将其转换为字符串会返回一个(空)字符串。 已解决:将编码更改为 NSASCIIStringEncoding,现在可以使用了。

NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"Received string: %@", responseString);

已解决的问题: 我正在尝试登录网站并获取返回的数据。 一切正常,重定向工作正常。

didReceiveData 也被多次调用,但奇怪的是:数据长度为零。即使我从 didReceiveData 中 NSLog 数据长度。

下面是 .m 和 .h 文件以及生成的 NSLog 信息。

我对 Objective-C 还是很陌生,现在我正在尝试解决这个问题几个小时,但我似乎找不到原因。

希望有人能帮我看看。

这是.m文件

#import "SiteConnection.h"

@implementation SiteConnection

@synthesize username;
@synthesize password;

// Other
- (void) printInstanceVars {

}

- (void) getInformation {
    // Enable Network Indicator
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

    // Set the request URL
    url = [NSURL URLWithString:@"http://url.com"];

    // Create the request using the URL defined above
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

    // Change User-Agent to a Browser
    [request setValue:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1" forHTTPHeaderField:@"User-Agent"];

    // Change method to POST
    [request setHTTPMethod:@"POST"];

    // Create POST string
    NSString *requestData = [NSString stringWithFormat:@"data=value"];

    // Append POST string to the request
    [request setHTTPBody: [requestData dataUsingEncoding:NSUTF8StringEncoding]];

    // Initialize the connection
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];

    // Start the connection
    [connection start];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    int statusCode = [httpResponse statusCode];

    // http statuscodes between 300 & 400 is a redirect ...
    if (response && statusCode >= 300 && statusCode < 400) {
        NSLog(@"Redirecting to : %@ (%u)", [request URL], statusCode);
    }

    return request;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0];

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSLog(@"Response: %u (%@)", [httpResponse statusCode], [NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    NSLog(@"Receiving data... Length: %d", [receivedData length]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error: %@", [error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // Hide Network Indicator
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    // Display data length
    NSLog(@"Total received data: %d", [receivedData length]);

    // Convert data into string and display it
    NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"Received string: %@", responseString);
    //NSLog(@"Cookies: %@", [[NSHTTPCookieStorage sharedHTTPCookieStorage] description]);
}

这是.h文件

#import <Foundation/Foundation.h>

@class StatusViewController;

@interface SiteConnection : NSObject {
    NSString *username;
    NSString *password;
    NSURL *url;
    NSMutableData *receivedData;
}

@property NSString *username;
@property NSString *password;

// Other
- (void) printInstanceVars;
- (void) getInformation;

@end

这是生成的 NSLog:

2012-07-02 15:41:12.578 Vodafone[25051:11303] Response: 200 (no error)
2012-07-02 15:41:12.578 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.580 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.580 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.582 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.583 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.585 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.586 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.587 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.590 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.590 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.591 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.592 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.596 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.601 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.605 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Receiving data... Length: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Total received data: 0
2012-07-02 15:41:12.608 Vodafone[25051:11303] Received string: 

【问题讨论】:

    标签: objective-c nsurlconnection nsmutabledata


    【解决方案1】:

    这样做:

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        if (!receivedData)
        {
            receivedData = [NSMutableData data];
        }
    
        [receivedData appendData:data];
    
        NSLog(@"Receiving data... Length: %d", [receivedData length]);
    }
    

    【讨论】:

    • 为什么是“else” appendData ... ?你只是松开第一帧。只是 if 并在所有情况下追加,而不是 if/else
    • 我多么愚蠢...阅读您的回答让我想到:啊,是的,当然。非常感谢!现在开始我的下一个问题。
    • luxsyher你知道receivedData是可变数据
    • 如果你问我,其实luxsypher 是对的。您的代码仅在收到第一个数据时创建一个空数据对象。这样您就可以丢弃第一个接收到的数据。
    【解决方案2】:

    看来你忘记了:

    NSMutableData *receivedData = [[NSMutableData alloc] init];
    

    【讨论】:

      【解决方案3】:

      在我看来你从未初始化receivedData。因此,您将收到的数据附加到一个 nil 指针,这显然没有效果;-) 在使用之前尝试receivedData = [[NSMutableData alloc] init]; 之类的内容。

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 2015-04-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-27
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        相关资源
        最近更新 更多