This article studies the consensus problem of multiagent systems (MASs) under denial-of-service (DoS) attacks and switching topology. Compared with the previous papers, under the switching topology, the impact of DoS attacks on the MAS is considered, and thus, the difficulty of consensus protocol design is further increased. First, a piecewise resilient consensus protocol is proposed to ensure that the MAS can achieve consensus under switching topology and DoS attacks. Then, by considering the different convergence rates caused by the switching topology in each sleep period of DoS attacks, a piecewise Lyapunov–Krasovskii function is established. The sufficient conditions on the global convergence of the system are obtained under switching topology and DoS attacks. In the meantime, the lower bound of average dwell time of the switching topology is determined under DoS attacks. The relationship between average dwell time and the parameters of DoS attacks is discussed. Finally, the simulation example is conducted to verify the effectiveness of the proposed method.