Jackson Heziner
Jackson Heziner

Reputation: 21

Android Jsoup Parsing URL for all Body Text

Situation: I have been attempting to parse a URL and retrieve the information between the body tags and setting it in the Android Text View.

Problem: Something is wrong and/or missing..

Code:

    package jsouptutorial.androidbegin.com.jsouptutorial;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.nodes.TextNode;
    import org.jsoup.select.Elements;
    import java.io.File;
    import java.io.IOException;
    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView textOut = (TextView)findViewById(R.id.rootTxtView);


//------------------Something went wrong here-------------------------------
            Document doc;
            try {
                //doc = Jsoup.connect("https://stackoverflow.com/questions/45311629/android-jsoup-parsing-url-for-all-body-text").get();
                doc = Jsoup.parse(new File("https://stackoverflow.com/questions/45311629/android-jsoup-parsing-url-for-all-body-text"), "UTF-8");

                Elements desc = doc.select("a.body");

                textOut.setText((CharSequence) desc);  //Setting textView to a String

            } catch (IOException e) {
                e.printStackTrace();
            }
//--------------------------------------------------------------------


        }
    }

Upvotes: 0

Views: 670

Answers (1)

Java Devil
Java Devil

Reputation: 10959

You have a couple of problems here:

First you are trying to create a File object from a URL, this will throw an IOException. You instead want to use the JSoup method to retrieve the document from the URL

Document doc = Jsoup.connect("https://stackoverflow.com/questions/45311629/android-jsoup-parsing-url-for-all-body-text").get();

The next problem is your elements selection doc.select("a.body"). This is trying to select all anchor tags <a> with a class of body - and there is none. To get the body just use doc.body()

Also as mentioned by cricket_007 you are attempting a network request from the Main thread so it will throw a NetworkOnMainThreadException the easiest way around this will be to run it in an AsyncTask, see this question for details.

Upvotes: 2

Related Questions