2013-07-11 26 views
0

我想从网站检索数据,我的应用程序不断崩溃。我已将问题定位到openStream()命令。我正在使用的类定义如下。我无法弄清楚我做错了什么。任何意见表示赞赏。Android的url.openStream()不工作

private class MyAsyncTask extends AsyncTask<Void, Void, Void>{ 

    //execute on background (out of the UI thread) 
    protected void doInBackground() { 
     URL url = null; 
     try { 
      url = new URL("URL is in here"); 

      BufferedInputStream bis = new BufferedInputStream(url.openStream()); 
      byte[] buffer = new byte[1024]; 
      StringBuilder sb = new StringBuilder(); 
      int bytesRead = 0; 
      while((bytesRead = bis.read(buffer)) > 0) { 
       String text = new String(buffer, 0, bytesRead); 
       sb.append(text); 
      } 
      bis.close(); 


     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

我在我的清单文件中有Internet权限。再次感谢任何帮助。

以下是在logcat中显示的错误。

11月7日至11日:26:31.110:E/AndroidRuntime(9259):致命异常:主 11月7日至11日:26:31.110:E/AndroidRuntime(9259):了java.lang.RuntimeException:无法开始活动ComponentInfo {com.example.annarbormap/com.example.annarbormap.MapActivity}:android.os.NetworkOnMainThreadException 07-11 11:26:31.110:E/AndroidRuntime(9259):at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2246) 07-11 11:26:31.110:E/AndroidRuntime(9259):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296) 07-11 11:26:31.110:E/AndroidRuntime(9259):at android.app.ActivityThread.access $ 700(ActivityThread.java:151) 07-11 11:26:31.110:E/Andr oidRuntime(9259):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1281) 07-11 11:26:31.110:E/AndroidRuntime(9259):at android.os.Handler.dispatchMessage(Handler。 java:99) 07-11 11:26:31.110:E/AndroidRuntime(9259):at android.os.Looper.loop(Looper.java:137) 07-11 11:26:31.110:E/AndroidRuntime 9259):at android.app.ActivityThread.main(ActivityThread.java:5293) 07-11 11:26:31.110:E/AndroidRuntime(9259):at java.lang.reflect.Method.invokeNative(Native Method) 07-11 11:26:31.110:E/AndroidRuntime(9259):在java.lang.reflect.Method.invoke(Method.java:511) 07-11 11:26:31.110:E/AndroidRuntime(9259): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102) 07-11 11:26:31.110:E/AndroidRuntime(9259):at com.android.inte rnal.os.ZygoteInit.main(ZygoteInit.java:869) 07-11 11:26:31.110:E/AndroidRuntime(9259):at dalvik.system.NativeStart.main(Native Method) 07-11 11:26 :31.110:E/AndroidRuntime(9259):导致:android.os.NetworkOnMainThreadException 07-11 11:26:31.110:E/AndroidRuntime(9259):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java: 1118) 07-11 11:26:31.110:E/AndroidRuntime(9259):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 07-11 11:26:31.110:E/AndroidRuntime(9259) :at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 07-11 11:26:31.110:E/AndroidRuntime(9259):at java.net.InetAddress.getAllByName(InetAddress.java:214) 07 -11 11:26:31.110:E/AndroidRuntime(9259):在libcore.net.http.HttpConnection。(HttpConnection。 java:70) 07-11 11:26:31.110:E/AndroidRuntime(9259):at libcore.net.http.HttpConnection。(HttpConnection.java:50) 07-11 11:26:31.110:E/AndroidRuntime (9259):在libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340) 07-11 11:26:31.110:E/AndroidRuntime(9259):位于libcore.net.http.HttpConnectionPool.get (HttpConnectionPool.java:87) 07-11 11:26:31.110:E/AndroidRuntime(9259):at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 07-11 11:26:31.110 :E/AndroidRuntime(9259):在libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 07-11 11:26:31.110:E/AndroidRuntime(9259):在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) 07-11 11:26:31.110:E/AndroidRuntime(9259):在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 07-11 11:26:31。110:E/AndroidRuntime(9259):在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 07-11 11:26:31.110:E/AndroidRuntime(9259):在libcore.net.http。 HttpEngine.sendRequest(HttpEngine.java:240) 07-11 11:26:31.110:E/AndroidRuntime(9259):在libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282) 07-11 11: 26:31.110:E/AndroidRuntime(9259):在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 07-11 11:26:31.110:E/AndroidRuntime(9259):位于libcore.net。 (HttpsURLConnectionImpl.java:271) 07-11 11:26:31.110:E/AndroidRuntime(9259) 26:31.110:E/AndroidRuntime(9259):at com.example.annarbormap.MapActivity $ MyAsyncTask.doInBackground(M apActivity.java:77) 07-11 11:26:31.110:E/AndroidRuntime(9259):at com.example.annarbormap.MapActivity.onCreate(MapActivity.java:48) 07-11 11:26:31.110: E/AndroidRuntime(9259):在android.app.Activity.performCreate(Activity.java:5250) 07-11 11:26:31.110:E/AndroidRuntime(9259):at android.app.Instrumentation.callActivityOnCreate(Instrumentation。 java:1097) 07-11 11:26:31.110:E/AndroidRuntime(9259):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) 07-11 11:26:31.110:E/AndroidRuntime 9259):...... 11多个

+0

请提供logcat或错误消息。 –

回答

4

NetworkOnMainThreadException

您创建了自己的doInBackground方法,但Internet连接只能在后台运行,请将代码放在覆盖方法doInbackground中!请参阅以下示例:

在后台实现获取数据的方法opPostExecute - 将数据设置为视图。

private class MyAsyncTask extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     URL url = null; 
     try { 
      url = new URL("URL is in here"); 

      BufferedInputStream bis = new BufferedInputStream(url.openStream()); 
      byte[] buffer = new byte[1024]; 
      StringBuilder sb = new StringBuilder(); 
      int bytesRead = 0; 
      while((bytesRead = bis.read(buffer)) > 0) { 
       String text = new String(buffer, 0, bytesRead); 
       sb.append(text); 
      } 
      bis.close(); 

     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return yourResult; 
    } 
} 

private class MyAsyncTask extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     doInBackground(); //your methods 
     return null; 
    } 

    protected void doInBackground() { 
     URL url = null; 
     try { 
      url = new URL("URL is in here"); 

      BufferedInputStream bis = new BufferedInputStream(url.openStream()); 
      byte[] buffer = new byte[1024]; 
      StringBuilder sb = new StringBuilder(); 
      int bytesRead = 0; 
      while((bytesRead = bis.read(buffer)) > 0) { 
       String text = new String(buffer, 0, bytesRead); 
       sb.append(text); 
      } 
      bis.close(); 


     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

}

希望,我帮你。