@@ -35,7 +35,7 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict {
3535 configuration.connectionProxyDictionary = proxyDict;
3636 }
3737 _httpManager = [[AFHTTPSessionManager alloc ] initWithSessionConfiguration: configuration];
38- _httpManager.responseSerializer = [AFJSONResponseSerializer serializer ];
38+ _httpManager.responseSerializer = [AFHTTPResponseSerializer serializer ];
3939 }
4040
4141 return self;
@@ -44,7 +44,7 @@ - (instancetype)initWithProxy:(NSDictionary *)proxyDict {
4444+ (QNResponseInfo *)buildResponseInfo : (NSHTTPURLResponse *)response
4545 withError : (NSError *)error
4646 withDuration : (double )duration
47- withResponse : (id ) responseObject
47+ withResponse : (NSData *) body
4848 withHost : (NSString *)host {
4949 QNResponseInfo *info;
5050
@@ -53,7 +53,7 @@ + (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
5353 NSString *reqId = headers[@" X-Reqid" ];
5454 NSString *xlog = headers[@" X-Log" ];
5555 int status = (int )[response statusCode ];
56- info = [[QNResponseInfo alloc ] init: status withReqId: reqId withXLog: xlog withHost: host withDuration: duration withBody: responseObject ];
56+ info = [[QNResponseInfo alloc ] init: status withReqId: reqId withXLog: xlog withHost: host withDuration: duration withBody: body ];
5757 }
5858 else {
5959 info = [QNResponseInfo responseInfoWithNetError: error host: host duration: duration];
@@ -67,22 +67,27 @@ - (void) sendRequest:(NSMutableURLRequest *)request
6767 __block NSDate *startTime = [NSDate date ];
6868 NSProgress *progress = nil ;
6969 __block NSString *host = request.URL .host ;
70+
7071 NSURLSessionUploadTask *uploadTask = [_httpManager uploadTaskWithStreamedRequest: request progress: &progress completionHandler: ^(NSURLResponse *response, id responseObject, NSError *error) {
72+ NSData *data = responseObject;
7173 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
7274 double duration = [[NSDate date ] timeIntervalSinceDate: startTime];
7375 QNResponseInfo *info;
7476 NSDictionary *resp = nil ;
7577 if (error == nil ) {
76- info = [QNSessionManager buildResponseInfo: httpResponse withError: nil withDuration: duration withResponse: responseObject withHost: host];
78+ info = [QNSessionManager buildResponseInfo: httpResponse withError: nil withDuration: duration withResponse: data withHost: host];
7779 if (info.isOK ) {
78- resp = responseObject;
80+ NSError *tmp;
81+ resp = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error: &tmp];
7982 }
8083 }
8184 else {
82- info = [QNSessionManager buildResponseInfo: httpResponse withError: error withDuration: duration withResponse: responseObject withHost: host];
85+ info = [QNSessionManager buildResponseInfo: httpResponse withError: error withDuration: duration withResponse: data withHost: host];
8386 }
87+ [progress removeObserver: self forKeyPath: @" fractionCompleted" context: (__bridge void *)(progressBlock)];
8488 completeBlock (info, resp);
8589 }];
90+ [progress addObserver: self forKeyPath: @" fractionCompleted" options: NSKeyValueObservingOptionNew context: (__bridge void *)(progressBlock)];
8691
8792 [request setTimeoutInterval: kQNTimeoutInterval ];
8893
@@ -91,6 +96,19 @@ - (void) sendRequest:(NSMutableURLRequest *)request
9196 [uploadTask resume ];
9297}
9398
99+ - (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context {
100+ if ([keyPath isEqualToString: @" fractionCompleted" ]) {
101+ NSProgress *progress = (NSProgress *)object;
102+ QNInternalProgressBlock progressBlock = (__bridge QNInternalProgressBlock)context;
103+ if (progress != nil && progressBlock != nil ) {
104+ progressBlock (progress.completedUnitCount , progress.totalUnitCount );
105+ }
106+ }
107+ else {
108+ [super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
109+ }
110+ }
111+
94112- (void )multipartPost : (NSString *)url
95113 withData : (NSData *)data
96114 withParams : (NSDictionary *)params
0 commit comments