在android studio中读取网站的html代码

时间:2019-03-14 09:36:43

标签: java android

我也清理并重建了代码,但是问题仍然没有解决。 下面是我的代码

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String result = null;
    String stringUrl = "https://www.ecowebhosting.co.uk/";
    DownloadTask downloadTask = new DownloadTask();

    downloadTask.execute(stringUrl);


}

public class DownloadTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String result = "";
        URL url;
        HttpURLConnection httpURLConnection = null;
        try {
            url = new URL(urls[0]);
            //It is like opening a browser
            httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream in = httpURLConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while (data != -1) {
                char currentChar = (char) data;
                result = result + currentChar;
                data = reader.read();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "Failed";
        }
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Log.i("Results",s);
    }
}

}

代码运行正常,但是日志中没有任何内容。以下是日志。

日志的屏幕截图

screenshot of the logs

2 个答案:

答案 0 :(得分:0)

AsynTask是一个异步过程。因此,当您调用Log.i("Result:", result);时,AsyncTask尚未完成,并且result仍然为空。

您应该通过onPostExecute()方法打印结果。 您可以查看this page

以下是一些有关如何正确实现AsyncTask的示例:

最佳

答案 1 :(得分:0)

您只需要在doInBackGround内部更改代码

    public class DownloadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls)
        {
            String result;
            String inputLine;
            try {
                URL myUrl = new URL(urls[0]);
                HttpURLConnection connection =(HttpURLConnection)
                        myUrl.openConnection();
                connection.setReadTimeout(150000); 
                connection.setConnectTimeout(15000); 
                connection.setRequestMethod("GET");
                connection.connect();
                InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
                //Create a new buffered reader and String Builder
                BufferedReader reader = new BufferedReader(streamReader);
                StringBuilder stringBuilder = new StringBuilder();
                //Check if the line we are reading is not null
                while((inputLine = reader.readLine()) != null){
                    stringBuilder.append(inputLine);
                }
                //Close our InputStream and Buffered reader
                reader.close();
                streamReader.close();
                //Set our result equal to our stringBuilder
                result = stringBuilder.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return "error";
            }
            return result;
        }
 @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Log.i("Results",s);
    }
    }