Android -从外部URL复制字符串-这是可能的


Android - Copy string from external URL - Is it possible?

我有一个应用程序,需要从一个外部URL,我不拥有的字符串。例如,我需要应用程序在外部html文件的文本中找到"点A"到"点B"之间的表达式,并获得2点之间的整个文本。

我认为可以通过webview搜索文本,但我在另一篇文章中读到,Android不允许我的应用程序复制在webview中找到的文本,然后粘贴到textview中。

那么,这是不可能的吗?我在想什么是更好的方法……你能给我一些建议吗?也许在我的服务器上有一个html/asp文件来做搜索,然后我会通过我的"网站搜索"工具得到字符串。

我不知道这是不是最好的办法,我无法想象我怎么能那样做。

谢谢你的建议

URL externalURL = new URL("http://stackoverflow.com/questions/40347333/android-
        copy-string-from-external-url-is-it-possible");
BufferedReader in = new BufferedReader(new InputStreamReader(externalURL.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
    response += inputLine + ''n';
}
in.close();
return response

在其他线程上运行这段代码,而不是主线程(AsyncTask将完成这项工作),做解析的事情,将response应用于TextView,你有你的外部url阅读器

我想你可以从WebView访问url。因此,您可以发出自己的请求来获取HTML并进行搜索。

类似:

new Thread(new Runnable(){
        @Override
        public void run() {
            URL url;
            HttpURLConnection connection = null;
            try {
                url = new URL("http://www.google.com");
                connection = (HttpURLConnection)url.openConnection();
                InputStream is = connection.getInputStream();
                InputStreamReader isw = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isw);
                StringBuilder builder = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    builder.append(line).append(''n');
                }
                String html = builder.toString();
                Log.d("HTML", html);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    }).start();