Android - 从外部网址复制字符串 - 是否可以?

时间:2016-10-31 17:27:05

标签: php android jquery html

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

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

那么,这样做不可能吗?我在考虑什么是更好的方法呢?你能给我一些建议吗?也许在我的服务器中使用html / asp文件进行搜索,然后我会通过我的网站搜索"工具。

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

感谢您提出任何建议。

2 个答案:

答案 0 :(得分:0)

我想您可以从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();

答案 1 :(得分:0)

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

在其他一些线程上运行这段代码而不是main(AsyncTask将完成工作),做解析的事情,将response应用到TextView并且你有你的外部网址阅读器。

相关问题