[英]Set HTTP Body to AFNetworking instead of using native request
Ive been recently trying to get AFNetworking to work as i did with native request, i have been always using AFNetworking for uploading POST or GET.but it never uploaded my image correct. 我最近一直在尝试让AFNetworking像本机请求一样工作,我一直在使用AFNetworking上载POST或GET。但是它从未正确上载我的图像。 it always uploaded to the server damaged and I've explained about the issue in this reference : Uploading image to server reached damaged using AFNetworking
它总是上载到损坏的服务器,在此参考资料中我已经解释了这个问题: 使用AFNetworking将图像上载到服务器已损坏
I've tried to use native NSURLConnection.sendSynchronousRequest
with HTTP Body and it has been uploaded and not damaged. 我尝试将本机
NSURLConnection.sendSynchronousRequest
与HTTP NSURLConnection.sendSynchronousRequest
一起使用,它已上传且未损坏。 and this code fully work but i need it in AFNetworking because it only supports IOS 8 + and for more reasons. 并且此代码完全可以正常工作,但是我在AFNetworking中需要它,因为它仅支持IOS 8+,并且出于其他原因。
let baseURL = ""
let selectedImage = self.userImage.image
let imageData = NSData(data: UIImageJPEGRepresentation(selectedImage!, 0.5)!)
let request = NSMutableURLRequest()
request.URL = NSURL(string: baseURL)
request.HTTPMethod = "POST"
request.addValue("image/jpeg", forHTTPHeaderField: "Content-Type")
let body = NSMutableData(data: imageData)
request.HTTPBody = body
do {
let responseData = try NSURLConnection.sendSynchronousRequest(request, returningResponse:nil)
let responseString = String(data: responseData, encoding: NSUTF8StringEncoding)
print("User image successfully uploaded navigating to home services")
} catch (let error as NSError) {
}
Following is a solution in Objective-c, replicate the same process for swift too. 以下是Objective-c中的解决方案,也可以快速复制相同的过程。 Create a object for
AFHTTPRequestOperationManager
as shown below. 如下所示,为
AFHTTPRequestOperationManager
创建一个对象。
[[AFHTTPRequestOperationManager alloc] initWithBaseURL:@"www.google.com"];
Now define NSDictionary
with key value pairs which will be parameters for your request body. 现在,使用键值对定义
NSDictionary
,这将是您的请求正文的参数。
NSDictionary *parameters = @{
@"paramaname": value,
@"paramaname":value
};
Now , when calling POST
request 现在,当调用
POST
请求时
[self.manager POST:@"someMethodName" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary * responseDict = (NSDictionary *)responseObject;
NSLog(@"responseDict : %@",responseDict);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error : %@ code %d",error.localizedDescription,error.code);
}
}];
This is how you can send POST request with parameters using AFNetworking
. 这是使用
AFNetworking
发送带有参数的POST请求的方法。
NSMutableDictionary *contentDictionary = [[NSMutableDictionary alloc]init];
[contentDictionary setValue:@"name" forKey:@"email"];
[contentDictionary setValue:@"name" forKey:@"username"];
[contentDictionary setValue:@"name" forKey:@"password"];
[contentDictionary setValue:@"name" forKey:@"firstName"];
[contentDictionary setValue:@"name" forKey:@"lastName"];
NSData *data = [NSJSONSerialization dataWithJSONObject:contentDictionary options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"%@",jsonStr);
NSString *urlString = [NSString stringWithFormat:@"http://testgcride.com:8081/v1/users"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"moinsam" password:@"cheese"];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:<block> failure:<block>];
try this one 试试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.