后臺(tái)是Java掠廓,今天他們要求和上傳Json到服務(wù)器,方法如下:
iOS 如何用post方式上傳son數(shù)據(jù)? ?— 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這個(gè)庫,內(nèi)容傳遞使用的是Json,至少在Url上是看不出內(nèi)容來的
順便提供一段使用系統(tǒng)內(nèi)置庫解析Json的代碼:NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[jsonResult dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
AFNetworking post json數(shù)據(jù)錯(cuò)誤在線求解答? ? 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數(shù)據(jù)錯(cuò)誤在線求解答
my.oschina.net/gexun/blog/316408
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; // 設(shè)置content-Type為text/html
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
關(guān)于使用AFN設(shè)置Content-Type的問題
【iOS】網(wǎng)絡(luò)操作與AFNetworking - xn4545945 http://blog.xigulu.com
- 博客頻道 - CSDN.NET
AFNnetworking詳解 - 守望遠(yuǎn)方 - 博客園
AFN post的數(shù)據(jù)編碼格式問題 - 推酷
IOS?7使用POST、GET提交JSON數(shù)據(jù)到服務(wù)器_陳宇_新浪博客
iOS 將NSArray与纽、NSDictionary轉(zhuǎn)換為JSON格式進(jìn)行網(wǎng)絡(luò)傳輸 -
- 博客頻道 - CSDN.NET