在 Android 中读取 URL 的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2075836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:25:27  来源:igfitidea点击:

Read contents of a URL in Android

android

提问by user200565

I'm new to android and I'm trying to figure out how to get the contents of a URL as a String. For example if my URL is http://www.google.com/I want to get the HTML for the page as a String. Could anyone help me with this?

我是 android 新手,我正在尝试弄清楚如何将 URL 的内容作为字符串获取。例如,如果我的 URL 是http://www.google.com/,我想以字符串的形式获取页面的 HTML。有人可以帮我解决这个问题吗?

回答by Drew

From the Java Docs : readingURL

来自 Java 文档:readingURL

URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
            new InputStreamReader(
            yahoo.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);

in.close();

Instead of writing each line to System.outjust append it to a string.

而不是写每一行System.out只是将它附加到一个字符串。

回答by detman

You can open a stream and read and append each line to a string - remember to wrap everything with a try-catch block - hope it helps!

您可以打开一个流并读取每一行并将其附加到一个字符串中 - 请记住用 try-catch 块包装所有内容 - 希望它有所帮助!

String fullString = "";
URL url = new URL("http://example.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
    fullString += line;
}
reader.close();

回答by live-love

You can put it in an AsyncTask like this:

你可以像这样把它放在一个 AsyncTask 中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    try {
        new Main2Activity.MyTask().execute(this);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static class MyTask extends AsyncTask<Object, Void, String> {

    Main2Activity activity;

    @Override
    protected String doInBackground(Object... params) {
        activity = (Main2Activity)params[0];
        try {
            StringBuilder sb = new StringBuilder();
            URL url = new URL("http://www.google.com/");

            BufferedReader in;
            in = new BufferedReader(
                    new InputStreamReader(
                            url.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
                sb.append(inputLine);

            in.close();

            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String str) {
        //Do something with result string
        WebView webView = activity.findViewById(R.id.web_view);
        webView.loadData(str, "text/html; charset=UTF-8", null);
    }

}