一部の (かなり古い) ASIHTTPRequest コードを AFNetworking V2.0 に更新しています。
現在、リクエスト用の追加パラメーターを使用して NSDictionary をアップロードする POST リクエストを処理しています。
ASIHTTPRequest コードは次のようになります。
NSMutableData* mPostData = [NSMutableData dataWithData:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSString *msgLength = [NSString stringWithFormat:@"%d", [postData length]];
[r setPostBody: mPostData];
[r addRequestHeader: @"Content-Length" value:msgLength];
}
postData は、キー/値を持つ NSDictionary です。これは、実行されたアクションに基づいています。たとえば、画像をアップロードすると、追加のパラメーターが含まれます。ユーザー登録を完了すると、さまざまなパラメーターが使用されますが、このコードが含まれているのと同じ方法を使用してください。
デリゲートは、次のコードを呼び出します。
//Request must be ASIFormDataRequest
- (BOOL) addFileWithPath:(NSString*) filePath fileName: (NSString*)fileName ofType: (NSString*) fileType withKey: (NSString*) fileKey uploadProgressDelegate:(id) uploadProgressDelegate
{
if ([request isKindOfClass:[ASIFormDataRequest class]])
{
ASIFormDataRequest *formRequest = (ASIFormDataRequest *) request;
NSLog(@"%@ %@ %@ %@",filePath,fileName,fileType,fileKey);
if (uploadProgressDelegate)
{
[formRequest setUploadProgressDelegate:uploadProgressDelegate];
}
NSLog(@"filename = %@",fileName);
[formRequest setFile:filePath withFileName:fileName andContentType:fileType forKey:fileKey];
return YES;
}
else
{
NSLog(@"WebService must be initialised with PostDataValuesAndKeys so that ASIFormDataRequest is made");
return NO;
}
}
今、私が行った掘り下げの量から-私はこのビットのコードしか見ることができAfNwtworking
ません-それはバージョン1.xからのもののように見えます
NSString *urlString = @"yourUrl";
NSURL* url = [NSURL URLWithString:urlString];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSURL *localVideoURL = [NSURL URLWithString:[userDefaults objectForKey:@"videoURL"]];
NSData *videoData = [NSData dataWithContentsOfURL:localVideoURL];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:videoData name:@"video_file" fileName:@"testvideo.mov" mimeType:@"video/quicktime"];
[formData appendPartWithFormData:[[BAUserInfoParser userInfoJson] dataUsingEncoding:NSUTF8StringEncoding] name:@"userInfo"];
[formData appendPartWithFormData:[[userDefaults objectForKey:@"transactionReceiptData"] dataUsingEncoding:NSUTF8StringEncoding] name:@"transactionData"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
// NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
float uploadPercentge = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
float uploadActualPercentage = uploadPercentge *100;
[lblUploadInfoText setText:[NSString stringWithFormat:@"%.2f %%",uploadActualPercentage]];
if (uploadActualPercentage >= 100) {
lblStatus.text = @"Waitting for response ...";
}
progressBar.progress = uploadPercentge;
}];
[httpClient enqueueHTTPRequestOperation:operation];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
lblStatus.text = @"Upload Complete";
NSData *JSONData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
NSLog(@"%@",error);
}];
[operation start];
}
-のバージョン 2 にはAFNetworking
、この種の新しい便利なメソッドがあります。
これは、追加のブロックを含む POST メソッドです。
[self.manager POST:url parameters:urlParameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// postData
}
} success:^(NSURLSessionDataTask *task, id responseObject) {
//Success
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//Failure
}];
しかし、私が問題を抱えているのは構築するBodyWithBlockです。NSDictionary オブジェクトを取得してそのブロックにアップロードするために何をする必要があるのか わかりません。