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);
}
}
}
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.