后台是Java,今天他们要求和上传Json到服务器,方法如下:
iOS 如何用post方式上传son数据 — ASI
{
NSString *newUrl = [[NSString alloc] initWithFormat:@"%@register/json", ABC];
NSURL *target = [[NSURL alloc]initWithString:newUrl];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
username, @"name",
password, @"password",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:target];
client.parameterEncoding = AFJSONParameterEncoding;
[client postPath:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self registerRequestFinished:operation];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self RequestFailed:error];
}];
}
这里使用的是AFNetworking这个库,内容传递使用的是Json,至少在Url上是看不出内容来的
顺便提供一段使用系统内置库解析Json的代码:NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonResult dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
AFNetworking post json数据错误在线求解答 NSString *url=@"http://crm.weichuan-sh.com.cn:5555/service/PADService.svc/getproducttaste"; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; NSDictionary *dict=@{@"pwd":@"crm2013.0",@"name":@"crm002"}; NSData *data=[NSJSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error: nil]; NSMutableData *jsonData=[NSMutableData dataWithData:data]; NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); [manager POST:url parameters:str success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"str = %@",str); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@",error); }];error = Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo=0x8e33450 {NSErrorFailingURLKey=http://crm.weichuan-sh.com.cn:5555/service/PADService.svc/getproducttaste, AFNetworkingOperationFailingURLResponseErrorKey={ URL: http://crm.weichuan-sh.com.cn:5555/service/PADService.svc/getproducttaste } { status code: 400, headers {
"Content-Length" = 1647;
"Content-Type" = "text/html";
Date = "Wed, 15 Oct 2014 09:13:20 GMT";
Server = "Microsoft-IIS/7.5";
"X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Request failed: bad request (400)}
AFNetworking post json数据错误在线求解答
my.oschina.net/gexun/blog/316408
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; // 设置content-Type为text/html
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
关于使用AFN设置Content-Type的问题
【iOS】网络操作与AFNetworking - xn4545945 http://blog.xigulu.com - 博客频道 - CSDN.NET
AFNnetworking详解 - 守望远方 - 博客园
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!-CSDN论坛-CSDN.NET-中国最大的IT技术社区
AFN post的数据编码格式问题 - 推酷
IOS 7使用POST、GET提交JSON数据到服务器_陈宇_新浪博客
iOS 将NSArray、NSDictionary转换为JSON格式进行网络传输 - - 博客频道 - CSDN.NET