0

这是我上传文件的 POST 请求。如何在实际发送之前对发送到服务器的 JSON 进行 NSLog 记录?

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:twitid forKey:@"twit"];
[dict setObject:hash forKey:@"hash"];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
if ([mediaFile isKindOfClass:[UIImage class]])
{
   imageData = UIImageJPEGRepresentation(mediaFile, 0.5); 
}

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {

if ([mediaFile isKindOfClass:[UIImage class]])
        {
          [formData appendPartWithFileData:imageData name:@"media" fileName:@"picture.png" mimeType:@"image/png"];  
        }
    else if(isThisAudio)
    {
       [formData appendPartWithFileData:mediaFile name:@"media" fileName:@"audio.caf" mimeType:@"audio/caf"];  
    }
    else
    {
        [formData appendPartWithFileData:mediaFile name:@"media" fileName:@"movie.mov" mimeType:@"video/quicktime"];  
    }

}];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request

success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
    NSDictionary *jsonDictionary = JSON;       
    NSLog(@"jsonDictionary: %@",jsonDictionary);
}
failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
{
    NSLog(@"Failed: %@",[error localizedDescription]);
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

    percentDone = ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite)); 
    NSLog(@"Uploaded %f",percentDone);
}];
[httpClient enqueueHTTPRequestOperation:operation];
4

2 回答 2

0

签出NSURLRequest-cURL类别。

使用该类别,您可以简单地:

NSLog(@"%s %@", __FUNCTION__, [request cURLCommand])
于 2013-07-03T13:30:53.310 回答
0

一个不错的选择是使用诸如Charles 代理之类的东西来监控实际通过网络发送的内容。通过这种方式,您可以看到网络上实际发生的情况,而不是代码流中某个点的状态。

于 2013-07-03T13:23:02.660 回答