0

Trying to practice and get info from website using Jsoup not the website API. My code does not have an error but the text field is not changing. It just shows me a blank space. How would i get info from the website? i'm trying to obtain the Main News so i could post on my website.

My Code:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.io.InputStream;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Document document;
    TextView text;
    String ankosh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView)findViewById(R.id.textView);
        new Ankosh().execute();

    }

    public class Ankosh extends AsyncTask<Void, Void, Void> {

        private Exception exception;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                // Connect to the web site
                document = Jsoup.connect("http://www.theguardian.com/us").get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void feed) {

            String ankosh = document.attr("href");
            text.setText(ankosh);



        }
    }


}
Richard
  • 560
  • 1
  • 8
  • 17
asdddd
  • 51
  • 1
  • 4
  • 1
    Practicing is nice but you are very likely violating the ToS. Also if you expect help, you should specify the exact part of html you need to parse. You cannot expect people here to visit the site to figure out what you could be looking for. – Filburt Jun 27 '16 at 07:56
  • Debug your code and see why. In this case the root tag of the html document simply does not (and should not) have an attribute called ``href``. That means that your ``ankosh`` variable is either null or an empty ``String``. Please learn how to use breakpoints and debugging. – f1sh Jun 27 '16 at 08:03

1 Answers1

0

The problem is lying here:

@Override
protected void onPostExecute(Void feed) {
    String ankosh = document.attr("href");
    text.setText(ankosh);
}

The document variable doesn't have an attribute called href. This is why the ankosh variable is empty.

Instead try this: (I suppose the main news if the first div with fc-item__content class in the document).

Element mainNewsDiv = document.select("div.fc-container--rolled-up-hide.fc-container__body > div:nth-child(1) > ul > li > div > div > div.fc-item__content").first();

if (mainNewsDiv == null) {
    // Main news not found...
} else {
    text.setText(mainNewsDiv.text());
}

One last note, you should avoid Jsoup.connect for loading the document. Internally, it uses the URL class which is notoriously slow. Use Volley instead. Please see this sample code showing the use of Volley and Jsoup.

Community
  • 1
  • 1
Stephan
  • 41,764
  • 65
  • 238
  • 329