android 网络请求图片,返回值为空的解决办法

android.os.NetworkOnMainThreadException

网上搜索后知道是因为版本问题,在4.0之后在主线程里面执行Http请求都会报这个错,也许是怕Http请求时间太长造成程序假死的情况吧。那么网上的朋友也给出了相应的解决方案,这叫上有政策下有对策:

一:在发起Http请求的Activity里面的onCreate函数里面添加如下代码:

//详见StrictMode文档
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

你可能感兴趣的:(android)