There's no easy answer. It's depressing to hear adult police officers behaving like high-schoolers in the locker room, talking smack about the public and so forth; then again it's not so different from hackers having a laugh at userland's expense. Policing in many places is a scary and dangerous job, and good officers are frequently insulted or blamed for the actions of some others in the same uniform.
I got a first hand taste of this by accident a few years ago. I was working on a film which was being shot in a recently decommissioned jail in Oakland, and one of the actors in a small part became unavailable. I had approximately the right look and was asked to fill in for the part in between my tech duties, so I spent the day in a police costume - movie uniforms are deliberately incorrect to avoid impersonation, but not so much that it's obvious. So between setups I was out on the street a few times to smoke or whatever, and boy did I get an earful from passers-by - and from the opposite folk you'd expect, if you went by stereotypes. A few people wanted to enter the jail to visit friends or relatives who had been arrested, and I would tell them the jail had been shut down and which number to call at City Hall for information. One irate person expressed the hope that someone would drive by and shut me down too. I stayed inside after that one.
Ideally, civilians should respect the police, and attempt to make a connection with those who are employed to serve them, while at the same time, the police should be accommodating for their constituents and dignify them much as civilians should.
The animosity can easily be attributed to the lack of respect and proper protocol for both parties. Someone needs to step up.
Alas, one can only hope for a better world.
If you're the man with the gun, you must go out of your way to be respectable and approachable, or you will not be - you'll just be a man with a gun.
Some of the hostility toward the police is justified, much of it isn't. But whether it is or isn't justified, it exists, and it's hard to blame cops for not going out of their way to be sociable with people who, more likely than not, hate them because they are police.