Developing high-performance non-precious metal electrocatalysts for oxygen reduction reaction (ORR) is crucial for the commercialization of fuel cells and metal-air batteries. However, doped carbon-based materials only show good ORR activity in alkaline medium, and become less effective in acidic environment. We believe that an appropriate combination of both ionic and electronic transport path, and well dopant distribution of doped carbon-based materials would help to realize high ORR performance under both acidic and alkaline conditions. Accordingly, a nitrogen and sulfur co-doped carbon framework with hierarchical through-hole structure is fabricated by morphology-controlled solid-state pyrolysis of poly(aniline-co-2-aminothiophenol) foam. The uniform high concentrations of nitrogen and sulfur, high intrinsic conductivity, and integrated three dimensional ionic and electronic transfer passageways of the 3D porous structure lead to synergistic effects in catalyzing ORR. As a result, the limiting current density of the carbonized poly(aniline-co-2-aminothiophenol) foam is equivalent to commercial Pt/C in acidic environment, and twice the latter in alkaline medium.