1 AFNetworking3.0如何post一个json数据到服务器
NSDictionary *dic = @{@"username":self.registerPhoneField.text,@"phone":self.registerPhoneField.text,@"psw":self.registerPasswordField.text};
// 初始化Manager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 不加上这句话,会报“Request failed: unacceptable content-type: text/plain”错误,因为我们要获取text/plain类型数据
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:@"http://10.1.130.77/login/submit" parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
// 这里可以获取到目前的数据请求的进度
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 请求成功,解析数据
NSLog(@"%@", responseObject);
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", dic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 请求失败
NSLog(@"%@", [error localizedDescription]);
}];
丰富版 加入了json转化代码
NSDictionary *dic = @{@"username":self.loginPhoneFiled.text,@"phone":self.loginPhoneFiled.text,@"psw":self.loginPasswordField.text};
// 初始化Manager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 不加上这句话,会报“Request failed: unacceptable content-type: text/plain”错误,因为我们要获取text/plain类型数据
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer =[AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager.requestSerializer setValue:@"application/json"forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json;charset=utf-8"forHTTPHeaderField:@"Content-Type"];
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody = data;
// post请求
[manager POST:@"http://10.1.130.77:8080/login/submit.do" parameters:dic constructingBodyWithBlock:^(id _Nonnull formData) {
// 拼接data到请求体,这个block的参数是遵守AFMultipartFormData协议的。
} progress:^(NSProgress * _Nonnull uploadProgress) {
// 这里可以获取到目前的数据请求的进度
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 请求成功,解析数据
NSLog(@"%@", responseObject);
NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", dic1);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 请求失败
NSLog(@"%@", [error localizedDescription]);
}];
2 AFNetworking2.0如何post一个json数据到服务器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//申明返回的结果是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer=[AFJSONRequestSerializer serializer];
//如果报接受类型不一致请替换一致text/html或别的
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//传入的参数
NSDictionary *parameters = @{@"1":@"XXXX",@"2":@"XXXX",@"3":@"XXXXX"};
//你的接口地址
NSString *url=@"http://xxxxx";
//发送请求
[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
3 NSURLConnection
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://10.1.130.77:8080/login/submit.do"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 2.设置请求头
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// 3.设置请求体
NSDictionary *json = @{
@"username" : self.loginPhoneFiled.text,
@"phone" : self.loginPasswordField.text,
@"psw" : self.loginPhoneFiled.text
};
// NSData --> NSDictionary
// NSDictionary --> NSData
NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody = data;
// 4.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
}];
4ASIHTTPRequest提交JSON数据
NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:@"0", @"Version", nil];
if ([NSJSONSerialization isValidJSONObject:user])
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
//NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);
NSURL *url = [NSURL URLWithString:@"http://42.96.140.61/lev_version.php"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request setPostBody:tempJsonData];
[request startSynchronous];
NSError *error1 = [request error];
if (!error1) {
NSString *response = [request responseString];
NSLog(@"Test:%@",response);
}
}