2

I have a website which only shows one line of text which I need to extract the text form in android studio, I would prefer to get it as a string. How do I do this?

Something such as webView.getTitle() would work but than for the content of the site, is there such a quick way to get this or how should I else do it?

specific info

the site I need to get the information form is:

 "<html> <head></head> <body> #4d636f </body> </html> "

from this I only need the text in the body, in this case a color as text.

jorisp
  • 31
  • 1
  • 6
  • 1
    Possible duplicate of [Retrieve data from website in android app](https://stackoverflow.com/questions/6761082/retrieve-data-from-website-in-android-app) – Ilmari Karonen Dec 25 '18 at 01:45

3 Answers3

4

You can use any Web Scraper/Crawler API to fetch data from web site.

For example: JSOUP API For Java And Android

Update

Step By Step guide to solve the mentioned problem

  1. Add Jsoup dependency to the app level of your build.gradle.

implementation 'org.jsoup:jsoup:1.11.1'

  1. Add Internet permission to the Android Manifest file for internet access.

<uses-permission android:name="android.permission.INTERNET" />

  1. Add button and text view in your app to get data from website on button click and display the result on text view.

Below is the sample code:

public class MainActivity extends AppCompatActivity {

    private TextView result;
    private Button fetch;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = (TextView) findViewById(R.id.result);
        fetch = (Button) findViewById(R.id.fetch);
        fetch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getBodyText();
            }
        });
    }
    private void getBodyText() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final StringBuilder builder = new StringBuilder();

                try {
                    String url="http://www.example.com";//your website url
                    Document doc = Jsoup.connect(url).get();

                    Element body = doc.body();
                    builder.append(body.text());

                } catch (Exception e) {
                    builder.append("Error : ").append(e.getMessage()).append("\n");
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        result.setText(builder.toString());
                    }
                });
            }
        }).start();
    }

}
Wando
  • 131
  • 1
  • 6
0

This type of process is known as web scrubbing. And you could do more research to see different methods. One methd I would suggest is getting the HTML from source and searching the DOM for any tags unique to the text you want.

By getting the HTML you avoid rendering the whole page (images, javascript, ect..)

Do you have a snippet of the source code you want to scrub from?

0

Sure here is an example. P.S. I'm not familiar with javascript, correct him for your case.

webView.evaluateJavascript("return document.getElementById(your_id)", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String value) {
        // value is your result
    }
});
Dmytro Ivanov
  • 1,260
  • 1
  • 8
  • 10
  • I tried this, and for the getElement I used "getElementByTagName("body")", since my site is only: " #4d636f ". but this gave me as value just null. what java should I than use? – jorisp Dec 30 '18 at 13:58