Sony Corp. posted an unexpected quarterly loss on Wednesday, demonstrating the depth of the problems facing its electronics business and deflating a sense of optimism that was reflected in a recent share-price rally. While Sony said recent restructuring measures undertaken by Chief Executive Kazuo Hirai are starting to pay off, the company’s eighth-straight quarter in the red highlights the shifting landscape of consumer electronics, as smartphones encroach on traditional gadgets.