Two possible solutions:
1. The 30 pin connector got some dirts and messing up the sensor, the iPhone thinks its connected to a radio dock. Take hard piece of paper, cover with 1 layer fabric and fold it, soak with alcohol rub and clean the charger port thoroughly. You can also use a toothbrush if you have one handy, but remeber not to put much pressure. Please refer to the picture to understand better.
If you charger doesn't connect properly, or doesn't charge properly due to loose connection, you can also fix this issue by cleaning two corners and other area by a pin or toothpick. Don't get surprised if you find a lot of lint in those areas, loll...
2. If first solution fails even after cleaning those pins properly, you should try cleaning the headphone port. Take a cotton swab, dip into alcohol rub, squeeze out the extra alcohol, hold your iPhone 4 or 4s up side down (so that extra alcohol doesn't drip inside) and clean the headphone port nicely.
Good luck.
PS: Don't forget to turn off your phones before trying any of these technics.