在Android应用中连接到网站并检索html

时间:2012-11-23 19:35:46

标签: android http url connect

我正在尝试连接Android中的URL地址以获取HTML代码。我正在关注这个tutorial。调用readStream函数时出错。这是日志中的错误输出:

11-23 20:24:22.572: W/WindowManager(151): Failure taking screenshot for (246x437) to layer 21010
11-23 20:24:23.122: W/System.err(698): android.os.NetworkOnMainThreadException
11-23 20:24:23.133: W/System.err(698):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-23 20:24:23.133: W/System.err(698):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-23 20:24:23.133: W/System.err(698):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-23 20:24:23.133: W/System.err(698):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-23 20:24:23.133: W/System.err(698):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-23 20:24:23.142: W/System.err(698):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-23 20:24:23.142: W/System.err(698):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-23 20:24:23.142: W/System.err(698):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-23 20:24:23.153: W/System.err(698):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-23 20:24:23.153: W/System.err(698):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-23 20:24:23.153: W/System.err(698):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
11-23 20:24:23.153: W/System.err(698):  at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
11-23 20:24:23.162: W/System.err(698):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-23 20:24:23.162: W/System.err(698):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-23 20:24:23.162: W/System.err(698):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-23 20:24:23.162: W/System.err(698):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
11-23 20:24:23.162: W/System.err(698):  at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
11-23 20:24:23.172: W/System.err(698):  at com.example.seminarska.Prevozi.onCreate(Prevozi.java:53)
11-23 20:24:23.172: W/System.err(698):  at android.app.Activity.performCreate(Activity.java:5008)
11-23 20:24:23.172: W/System.err(698):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-23 20:24:23.172: W/System.err(698):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-23 20:24:23.172: W/System.err(698):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-23 20:24:23.182: W/System.err(698):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-23 20:24:23.182: W/System.err(698):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-23 20:24:23.182: W/System.err(698):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 20:24:23.194: W/System.err(698):  at android.os.Looper.loop(Looper.java:137)
11-23 20:24:23.194: W/System.err(698):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-23 20:24:23.203: W/System.err(698):  at java.lang.reflect.Method.invokeNative(Native Method)
11-23 20:24:23.212: W/System.err(698):  at java.lang.reflect.Method.invoke(Method.java:511)
11-23 20:24:23.212: W/System.err(698):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-23 20:24:23.212: W/System.err(698):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 20:24:23.223: W/System.err(698):  at dalvik.system.NativeStart.main(Native Method)

以下代码是我正在使用的代码。

try {
  URL url = new URL("http://www.vogella.com");
  HttpURLConnection con = (HttpURLConnection) url
    .openConnection();
  readStream(con.getInputStream());
  } catch (Exception e) {
  e.printStackTrace();
}



private void readStream(InputStream in) {
  BufferedReader reader = null;
  try {
    reader = new BufferedReader(new InputStreamReader(in));
    String line = "";
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (reader != null) {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
        }
    }
  }
} 

任何想法有什么不对?是否有不同的方式连接到网络。

1 个答案:

答案 0 :(得分:2)

您收到此错误是因为您正在进行网络操作,例如在主UI线程上检索HTML。

要以最佳方式解决此问题,请将所有与网络相关的代码移到单独的ThreadAsyncTask