Android:4.0 ICS上奇怪的openStream

时间:2012-03-21 16:20:53

标签: java android http inputstream

我尝试从文本视图中显示的网站上获取文字。

我使用:

try {
         textUrl = new URL("http://google.com");
         BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
         String StringBuffer;
               String stringText = "";
         while ((StringBuffer = bufferReader.readLine()) != null) {
          stringText += StringBuffer;
         }
               bufferReader.close();

        } catch (MalformedURLException e) {

         e.printStackTrace();

        } catch (IOException e) {
         e.printStackTrace();

        }

如果我将<uses-sdk android:targetSdkVersion>设置为15(ICS 4.0.3),那么我的应用程序崩溃了。 如果我删除该行(意味着目标是1.6我猜),那么它工作正常,但我的应用程序在我的手机上是奇怪的。

Logcat只是告诉我它无法启动Activity。

// Pew Labs

LOG:

03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:703)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.widget.TabHost.setCurrentTab(TabHost.java:350)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.view.View.performClick(View.java:3549)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.view.View$PerformClick.run(View.java:14400)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.os.Handler.handleCallback(Handler.java:605)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.os.Looper.loop(Looper.java:154)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.app.ActivityThread.main(ActivityThread.java:4944)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at java.lang.reflect.Method.invoke(Method.java:511)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at dalvik.system.NativeStart.main(Native Method)
03-21 17:12:42.874: E/AndroidRuntime(2101): Caused by: android.os.NetworkOnMainThreadException
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at com.pewlabs.sunds.Food.downloadFile(Food.java:64)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at com.pewlabs.sunds.Food.onCreate(Food.java:45)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.app.Activity.performCreate(Activity.java:4524)
03-21 17:12:42.874: E/AndroidRuntime(2101):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)

1 个答案:

答案 0 :(得分:1)

抛出的NetworkOnMainThreadException试图告诉您正在主UI线程上进行网络操作。您需要在单独的Thread中执行这些操作,或者简单的约定是使用AsyncTaskLoader