2016-04-14 119 views
0

我測試此功能,但不工作 清單文件中的權限可以確定 我做錯了什麼?在Android Studio中獲取HTML源代碼

public void getHTML() throws Exception { 
    URL url = new URL("http://www.android.com/"); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    try { 
     BufferedReader br = new BufferedReader(
       new InputStreamReader(urlConnection.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 

     while ((inputLine = br.readLine()) != null) { 
      response.append(inputLine); 
     } 
     txtInfo.setText("123"); 
     br.close(); 
    }finally { 
     urlConnection.disconnect(); 

    } 
} 
+0

你能上什麼 「不工作」 的意思解釋一下?錯誤信息?堆棧跟蹤?預期結果與觀察結果? –

+0

當我執行此代碼時,textview不會更改文本。在此行之前發生錯誤:txtInfo.setText(「123」); – user2022323

回答

0
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

你也可以使用AsynTask