如何在发送之前使用NSLog JSON

时间:2013-07-03 13:21:17

标签: ios objective-c cocoa-touch afnetworking

这是我上传文件的POST请求。我怎样才能在实际发送之前NSLog我发送到服务器的JSON?

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

结帐NSURLRequest-cURL类别。

您可以简单地使用类别:

NSLog(@"%s %@", __FUNCTION__, [request cURLCommand])