android.os.NetworkOnMainThreadException

运行环境:

Android Version:4.4.2

11-04 02:27:43.679    1333-1333/com.jast.menu_drawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.jast.menu_drawer, PID: 1333
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.jast.menuDrawer.utils.HttpUtils.sendPostMessage(HttpUtils.java:24)
            at com.jast.menuDrawer.fragment.ArticleFragment.getData(ArticleFragment.java:53)
            at com.jast.menuDrawer.fragment.ArticleFragment.onCreateView(ArticleFragment.java:40)
            at android.app.Fragment.performCreateView(Fragment.java:1700)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.BackStackRecord.run(BackStackRecord.java:684)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

问题分析:

官网API此处

Class Overview

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 documentDesigning for Responsiveness.

Also see StrictMode.

stackoverflow的分析

iteye

总结:

在主线程中访问网络。

解决方案:

不要再主线程中访问网络。

通过实验,使用AsyncTask:这个还是比较好的(不知道用什么词来形容此刻心情),代码如下:

public class myAsyncTask extends AsyncTask<String,Void,List<HashMap<String, Object>>>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected List<HashMap<String, Object>> doInBackground(String... params) {
            JSONObject jobj = JSON.parseObject(HttpUtils.sendPostMessage(params[0], "UTF-8"));
            JSONArray arr = jobj.getJSONArray("LatestArticles");
            List<Article> articles = JSON.parseArray(arr.toString(),Article.class);
            ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map = null;
            for(Article article:articles){
                map = new HashMap<String, Object>();
                map.put("article_title",article.getTitle());
                map.put("article_PubDate",article.getPubDate());
                map.put("article_category",article.getCategory());
                map.put("article_articleNo",article.getArticleNo());
                list.add(map);
            }
            return list;
        }

        @Override
        protected void onPostExecute(List<HashMap<String, Object>> result) {
            adapter = new SimpleAdapter(
                    rootView.getContext(),
                    result,
                    R.layout.article_list_item,
                    new String[]{"article_title", "article_PubDate", "article_category", "article_articleNo"},
                    new int[]{R.id.article_title, R.id.article_PubDate, R.id.article_category, R.id.article_articleNo});
            articleItem.setAdapter(adapter);
        }
    }



完美呈现!!

你可能感兴趣的:(android.os.NetworkOnMainThreadException)