Android调用服务器JSON数据出错
从网上下载的android程序demo可以调用json数据
但是当自己创意一个android程序时就会报错
希望能够得到您的帮助
Answers
在android4.0后的版本中,请求网络的时候用到StrictMode中AndroidBlockGuardPolicy的检查策略,如果在UI线程连接网络就会报这个异常,你可以尝试了一下把AndroidMainfest中的android:targetSdkVersion=”15″更换成更低的版本,这样就不会报错了。但是还是建议你把请求网络这些耗时的操作放到非UI线程中进行操作,建议你去看看AsyncTask这个类,我现在在开发中都是继承AsyncTask类实现它的doInBackground方法就行了。
最后贴出官方对这个 NetworkOnMainThreadException 的说明
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness.
Also see StrictMode.