18

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, what will be the approach to get source code of webpage by calling a url in android. 2) I would be having two layouts for source code in one layout and other for the webpage itself. If i am changing title tag value in source code layout, its must be automatically updated on actual webpage ?

What would be the best approach to do that in android ?

Any kind of help will be highly appreciated.

mH16
  • 2,356
  • 4
  • 25
  • 35

3 Answers3

12

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);
            }
        });
    }
}
Destiner
  • 540
  • 4
  • 16
Shahzad Afridi
  • 2,058
  • 26
  • 24
  • Surprisingly - I did not not about `ion`, and it's still under development. – elcuco Jul 25 '19 at 08:16
  • In today's version of Android Studio follow this path -> Click on the File, then Project Structure then Dependencies then the app and then click on the "+" icon of all dependencies sections and then choose library and type ion and select com.koushikdutta.ion and click ok, ok and you are good to go. – Kanha Tomar Aug 07 '22 at 16:36
1

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

Amjad sibili
  • 580
  • 1
  • 7
  • 15
1

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();
Tony
  • 259
  • 3
  • 13