Cannot connect using httpconnection in android

倾然丶 夕夏残阳落幕 提交于 2019-12-01 20:06:38

It gives networkonmainthread exception

This means that you are doing network operations on the main thread, and that's not allowed in the Strict Mode.

"In the main thread" usually means that you are doing it in the event handlers, like onResume, onPause, or in the onClick method of your OnClickListener. You may look at this post (Painless threading) to understand what's the problem and why this is not allowed. Generally, if you perform network operations in the main thread, it would block the processing of GUI events, and if the network operation was slow, your application would be not responsive.

Strict Mode appeared in API level 9, so this explains that why you have problem if min SDK version is 10, and why it works with min SDK version 8.

You should use AsyncTask or other methods to do it in the background. You may refer to 1 for this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!