因此,我在線學習一些教程以瞭解如何製作應用程序。 和我得到這個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)
你的堆棧跟蹤不會從該代碼的到來。 – CommonsWare
你有沒有機會'自動構建'禁用,如果在日食? – km86