我可以通过 AFHttpClient 制作网络服务,但由于库新版本不允许。请帮我创建帖子。因为我可以使用参数传递字典,但我想将其作为帖子字符串发送。
示例代码将非常有用,因为我已经在互联网上搜索过,无法提出请求。 //这是代码,但它在AFhttpClient
中 -(void)reportBullyWithAfnetworking{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] initWithURL:[NSURL URLWithString:BullyAlertUrlReport] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:180.0];
NSString* getLoginData;
NSLog(@"getLoginData %@", getLoginData);
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[AppDelegate getAppDelegate].mutableCopy options:NSJSONWritingPrettyPrinted error:&error];
getLoginData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
int contentLength = [getLoginData lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSString* contentLengthStr = [[NSString alloc] initWithFormat:@"%d", contentLength];
[request setHTTPMethod:@"POST"];
[request setValue:contentLengthStr forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[NSData dataWithBytes:[getLoginData UTF8String] length:contentLength]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:BullyAlertUrlReport]];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
NSString* responseString=[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@",responseString);
//[SVProgressHUD showSuccessWithStatus:@"Great Success!"];
SBJSON *jsonParser=[[SBJSON alloc]init];
NSMutableDictionary *response=(NSMutableDictionary *)[jsonParser objectWithString:operation.responseString error:nil];
NSLog(@"resposeCode = %@",response);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"error code %d",[operation.response statusCode]);
}];
[operation start];
}