Jericho, the first city conquered by Israel, was the first to suffer the consequences of 500 years or more of sin. Ai was the second, but only after disobedience in Israel had been dealt with. One man had kept some of the loot from Jericho for himself, disobeying the instructions to either destroy it or give it to the Levites. God, of course, knew:
Joshua 7:11 Israel has sinned;they have violated my covenant, which I commanded them to keep. They have taken some of the devoted things; they have stolen, they have lied, they have put them with their own possessions.
With the result that the Israelites were routed in their first attempt to take Ai:
Joshua 7:12 That is why the Israelites cannot stand against their enemies; they turn their backs and run because they have been made liable to destruction. I will not be with you anymore unless you destroy whatever among you is devoted to destruction.
Poor Achan: he found the hard way to discover what true obedience required. After he was dealt with, the Israelites successfully took Ai. Joshua then reminded the people where these victories come from:
Joshua 8:34 Afterward, Joshua read all the words of the law—the blessings and the curses—just as it is written in the Book of the Law.