2016-03-03 62 views
-2

因此,我在線學習一些教程以瞭解如何製作應用程序。 和我得到這個NetworkOnMainThreadException坡平了,我做了一些researc,我發現我不得不創建一個AsynTask類,並覆蓋了Methode doInBackground,我這樣做,但我還是得到了MainThreadExceptionNetworkOnMainThreadException當在AsyncTask類中調用方法

你們可以點我出了什麼我做錯了?爲什麼它錯了?

這裏是我的代碼:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_list); 

     GetPostTask getPost = new GetPostTask(); 
     getPost.execute(); 


//  Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show(); 
    } 

和AsynTask類:

private class GetPostTask extends AsyncTask<Object, Void, String> { 

     @Override 
     protected String doInBackground(Object... params) { 
      int responseCode = -1; 

      try { 
       URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS); 
       HttpURLConnection connection = (HttpURLConnection)blogFeedUrl.openConnection(); 
       connection.connect(); 

       responseCode = connection.getResponseCode(); 
       Log.i(TAG, "Code" + responseCode); 

      } catch (MalformedURLException e) { 
       Log.e(TAG, "Error making URL : ", e); 
      } catch (IOException e) { 
       Log.e(TAG, "Error making Connection : ", e); 
      } catch (Exception e) { 
       Log.e(TAG, "Error General ====================================: ", e); 
      } 
      return "Code :" + responseCode; 
     } 
    } 

給我這個錯誤:

03-03 19:40:51.900 2119-2119/? E/MainListActivity﹕ Error General ====================================: 
    android.os.NetworkOnMainThreadException 
      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) 
      at java.net.InetAddress.lookupHostByName(InetAddress.java:431) 
      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
      at java.net.InetAddress.getAllByName(InetAddress.java:215) 
      at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
      at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188) 
      at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157) 
      at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100) 
      at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357) 
      at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340) 
      at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) 
      at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 
      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433) 
      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114) 
      at com.onarap.blogread.MainListActivity.onCreate(MainListActivity.java:27) 
      at android.app.Activity.performCreate(Activity.java:6237) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+3

你的堆棧跟蹤不會從該代碼的到來。 – CommonsWare

+0

你有沒有機會'自動構建'禁用,如果在日食? – km86

回答

1

你確定你正在運行正確的代碼?

你的堆棧跟蹤表明你是從的onCreate使網絡電話:

at com.onarap.blogread.MainListActivity.onCreate(MainListActivity.java:27) 

確保你正在建設你張貼在這裏以及網絡通話僅在doInBackground方法相同的代碼。

+0

哦,是的,我甚至沒有意識到這一點。 但我該如何管理?看起來好像我的文件被保存了,我沒有任何其他代碼被打開,並且當我在Run上運行時,它說這個調用是在MainListActivity.java:27上進行的,當時沒有寫在第27行:/ –

+0

Clean您的構建以確保您運行正確的代碼。 – Francesc

0

試試這個:

getPost.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 
相關問題