Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HTML source code from url in android?

Tags:

android

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.

like image 452
mH16 Avatar asked Jun 28 '11 08:06

mH16


People also ask

How can I see HTML source code in mobile?

Steps to view source code Visit the website which you want to view it's source code. Click inside the address bar and append view-source: before the url of the website. Click the send button and the page will load with the source code of the file.

How do I get the HTML code from a website?

Open the page in a new browser window and then highlight a section of the page using the mouse. Right-click and then select INSPECT from the dropdown menu. This will open the developer tools and show you the HTML code associated with the particular element or page section.


3 Answers

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);
            }
        });
    }
}
like image 190
Shahzad Afridi Avatar answered Oct 14 '22 03:10

Shahzad Afridi


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

like image 23
Amjad sibili Avatar answered Oct 14 '22 05:10

Amjad sibili


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();
like image 1
Tony Avatar answered Oct 14 '22 04:10

Tony