How to get HTML source code from url in android?

前端 未结 3 941
悲哀的现实
悲哀的现实 2020-11-29 04:01

I am working on project to get html source code in a string in vb.net using dom parser to get source code of a page.

1) I want to implement the same in android, wha

相关标签:
3条回答
  • 2020-11-29 04:35

    You can go for Web scraping techniques in Java. There are plenty of libraries available which I found simple and robust is a library called jaunt . You can read the documentation here

    0 讨论(0)
  • 2020-11-29 04:42

    You can get Html code from any URL by using ion library.

    Go to the project structure, click on app, click on Dependencies, click on '+', just type ion you will see com.koushikdutta.ion:ion:2.1.8 click it and click ok. Now you can use ion library to get html code from URL.

    public class HtmlCode extends Activity {
        TextView tv;
    
        public void onCreate(Bundle s)
        {
            super.onCreate(s);
            setContentView(R.layout.httpexample);
    
            tv = (TextView)findViewById(R.id.tvHttp);
            Ion.with(getApplicationContext()).load("http://www.your_URL.com").asString().setCallback(new FutureCallback<String>() {
                @Override
                public void onCompleted(Exception e, String result) {
    
                    tv.setText(result);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-29 04:47

    Try this:

        URL google = null;
        try {
            google = new URL("https://www.google.com");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(google.openStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String input = null;
        StringBuffer stringBuffer = new StringBuffer();
        while (true)
        {
            try {
                if (!((input = in.readLine()) != null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            stringBuffer.append(input);
        }
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String htmlData = stringBuffer.toString();
    
    0 讨论(0)
提交回复
热议问题