android下的android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork錯誤 -开发者知识库

android下的android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork錯誤 -开发者知识库,第1张

今天想在Android下,用httpClient去獲取網頁的內容,結果在httpClient處報了錯,

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145), libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84), libcore.io.IoBridge.connectErrno(IoBridge.java:127), libcore.io.IoBridge.connect(IoBridge.java:112), java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192), java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459), java.net.Socket.connect(Socket.java:843), org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119), org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144), org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164), org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119), org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360), org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555), org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl

問我們的Android大神,得知Android不能再主線程去訪問網絡,需要另起線程去訪問

方法一:

   @TargetApi(Build.VERSION_CODES.GINGERBREAD)

        @SuppressLint("NewApi")

        public static String getHtmlByUrl(String url){  

       StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复