我正在尝试通过下载网页的html,AsyncTask然后在日志中显示该html。
这是我的代码。但是,当我运行代码时,循环永远不会停止。
public class MainActivity extends AppCompatActivity { public class DownloadTask extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... urls) { String result = ""; HttpURLConnection connection = null; URL myUrl; try{ myUrl = new URL(urls[0]); connection = (HttpURLConnection) myUrl.openConnection(); InputStream in = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(in); int data = reader.read(); while(data != -1){ char current = (char) data; result += current; data = reader.read(); } return result; } catch(Exception e){ e.printStackTrace(); return "Failed"; } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DownloadTask task = new DownloadTask(); try { String result = task.execute("http://www.posh24.com/celebrities").get(); Log.i("asd",String.valueOf(result)); } catch(Exception e){ e.printStackTrace(); } } }
我的日志充满了:
D/dalvikvm: GC_FOR_ALLOC freed 297K, 20% free 2582K/3200K, paused 4ms, total 4ms
任何想法代码有什么问题吗?