IOS 对接Java后台AFNetWork 的json报异常

转载自https://www.jianshu.com/p/0f6fac1cf62f

// if (![NSJSONSerialization isValidJSONObject:parameters]) {// if (error) {// NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedStringFromTable(@"The `parameters` argument is not valid JSON.", @"AFNetworking", nil)};// *error = [[NSError alloc] initWithDomain:AFURLRequestSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo];// }// return nil;// }// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error];//// if (!jsonData) {// return nil;// }//// [mutableRequest setHTTPBody:jsonData];


隐掉上面这些代码,上面主要是做了json的检验,会抛异常,Java 的json规则貌似和OC的有差异,所以建议隐藏掉上面的,加上下面新的代码

//新加的if([parameters isKindOfClass:[NSString class]]){[mutableRequest setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];}else{[mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];}


{

"deleteIds": [],

"searchType": "1"

}

后台要的格式终于发送给Java后台了

你可能感兴趣的:(IOS 对接Java后台AFNetWork 的json报异常)