1

在我的应用程序中,我使用 HTTP post 方法上传了一张照片,并从服务器获取响应并向用户显示状态。我的代码在 ios5 中运行良好,但是当我使用 ios6 运行时,它不会触发委托方法“ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data”。它在我的应用程序中经常发生。当我运行应用程序时,我第一次触发上传按钮时它可以正常工作,而当我触发上传按钮时它不会调用委托方法。最后我粘贴了我的控制台日志。我在这里粘贴了我的代码供您参考。请帮我解决这个问题。提前致谢...

为什么我在这里需要didReceiveData委托方法,我必须根据我上传的文件解析服务器响应。服务器响应只找到正确的didReceiveData方法。

-(IBAction)upload:(id)sender
{
UIImage *test = [UIImage imageNamed:@"bottle.jpg"];
NSData *dataObj  = UIImageJPEGRepresentation(test, 1.0);

NSString *postLength = [NSString stringWithFormat:@"%d", [dataObj length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

NSString *requestUrl =[API_URL stringByAppendingString:@"/abcDemo_uploadPhoto"];
[request setURL:[NSURL URLWithString:requestUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"image/jpg" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"123" forHTTPHeaderField:@"UserId"];
[request setHTTPBody:dataObj];

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn)
{
    receiveData = [[NSMutableData data] retain];
}

}

- (void)connection:(NSURLConnection *)connection   didSendBodyData:(NSInteger)bytesWritten
 totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{

float progress = [[NSNumber numberWithInteger:totalBytesWritten] floatValue];
float total = [[NSNumber numberWithInteger: totalBytesExpectedToWrite] floatValue];

NSLog(@"progress/total %f",progress/total);

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse");
    [receiveData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"didReceiveData");
    [receiveData appendData:data];
}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
     NSLog(@"connectionDidFinishLoading");
 }

 - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
     NSLog(@"didFailWithError");    
 }

我的控制台日志:

第一次:

2013-01-10 06:24:41.446 SampleProject[1004:11c03] progress/total 1.000000
2013-01-10 06:24:41.893 SampleProject[1004:11c03] didReceiveResponse
2013-01-10 06:24:41.894 SampleProject[1004:11c03] didReceiveData
2013-01-10 06:24:41.894 SampleProject[1004:11c03] connectionDidFinishLoading

第二次:

2013-01-10 06:24:49.535 SampleProject[1004:11c03] progress/total 1.000000
2013-01-10 06:24:49.605 SampleProject[1004:11c03] didReceiveResponse
2013-01-10 06:24:49.606 SampleProject[1004:11c03] connectionDidFinishLoading

第三次:

2013-01-10 06:24:41.446 SampleProject[1004:11c03] progress/total 1.000000
2013-01-10 06:24:41.893 SampleProject[1004:11c03] didReceiveResponse
2013-01-10 06:24:41.894 SampleProject[1004:11c03] didReceiveData
2013-01-10 06:24:41.894 SampleProject[1004:11c03] connectionDidFinishLoading

第 4 次:

2013-01-10 06:24:49.535 SampleProject[1004:11c03] progress/total 1.000000
2013-01-10 06:24:49.605 SampleProject[1004:11c03] didReceiveResponse
2013-01-10 06:24:49.606 SampleProject[1004:11c03] connectionDidFinishLoading
4

1 回答 1

0

我认为您需要返回并开始检查 HTTP 响应。查看从服务器发回的状态代码和标头。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse");
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        NSHTTPURLResponse *HTTPResponse;
        NSLog(@"HTTP/1.X %d\n%@", [HTTPResponse statusCode],
            [[HTTPResponse allHeaderFields] componentsJoinedByString:@"\n"]);
    }
    [receiveData setLength:0];
}

您可能会看到类似 302 或某种 400 或 500 级别的错误。

于 2013-01-10T12:52:19.927 回答