写json格式时需注意
用NSString写json时,每一个引号前面要加“\”
异步代码
-(void)afHTTPGetData:(NSString *)url { //这个方法用AFNetworking 框架
NSLog(@"%@",url);
NSLog(@"afnetworking request.");
NSURL *httpurl = [[NSURL alloc] initWithString:url];
NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:httpurl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
NSString *str1 = @"{\"biz\":{\"key\":\"-1\",\"pageSize\":\"10\",\"pageStartRow\":\"0\"},\"sys\":{\"appcode\":\"moa\",\"compressdata\":false,\"encryptdata\":\"true\",\"keycode\":\"5yvfrapr8552006067505393265302\",\"method\":\"report\"}}";
NSMutableDictionary *dict1=[NSMutableDictionary dictionary];
[dict1 setObject:str1 forKey:@"xmas-json"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:httpurl];
[manager POST:url parameters:dict1 success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSString *jsonString = jsonData[@"dataString"];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableLeaves
error:nil];
NSArray *resourceArray = dic[@"resultResourceList"];
NSLog(@"123%@",resourceArray);
NSMutableArray *array = [NSMutableArray array];
for (NSArray *arr in resourceArray)
{
for (NSDictionary *arr1 in arr)
{
NSString *chargrUnit =[arr1 objectForKey:@"commodityName"];
NSLog(@"req responseObject %@",chargrUnit);
NSString *photoURlString=[NSString stringWithFormat:@"http://go.bgzy.com /webdocs/baozy/upload_image_mobile_size1/%@",[arr1 objectForKey:@"img_Path"]];
[Marr1 addObject:[ NSURL URLWithString:photoURlString]];
NSLog(@"req responseObject %@",photoURlString);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (error) {
NSLog(@"req error %@",error);
}
}];
}
-(void)afHTTPGetData123:(NSString *)url {
//NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
BOOL isTrue = false;
NSURL *url1 = [NSURL URLWithString:url];
NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:url1];
urlrequest.HTTPMethod = @"POST";
NSString *bodyStr1 = @"{\"biz\":{\"key\":\"-1\",\"pageSize\":\"10\",\"pageStartRow\":\"0\"},\"sys\":{\"appcode\":\"moa\",\"compressdata\":false,\"encryptdata\":\"true\",\"keycode\":\"5yvfrapr8552006067505393265302\",\"method\":\"report\"}}";
NSMutableDictionary *dict1=[NSMutableDictionary dictionary];
NSString *bodyStr = [NSString stringWithFormat:@"xmas-json=%@",bodyStr1];
NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
urlrequest.HTTPBody = body;
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlrequest];
requestOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
[requestOperation start];
[requestOperation waitUntilFinished];
NSLog(@"RESPONSE%@",requestOperation.responseString);
NSRange range = [requestOperation.responseString rangeOfString:@"\"msg\":\"0\""];
if (range.location != NSNotFound) {
isTrue = true;
}
if (!isTrue) {
NSLog(@"错误", @"您需要联系开发人员");
}
}