Reputation: 29
I need to access a url and pull some information from it. I am using Android Studio. I have code that does not throw any errors, but it is displaying no information. I believe the problem is probably that I am searching for the wrong parameter with my .select statement. Please keep in mind that I am very new to java/android development. Here is my code:
private class FetchAnton extends AsyncTask<Void, Void, Void> {
String price;
String url = "http://www.antoncoop.com/markets/cash.php";
@Override
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect(url).get();
price = String.valueOf(document.select("quotes['KEH15']"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView priceTextView = (TextView) findViewById(R.id.priceTextView);
priceTextView.setText(price);
}
}
And here is the HTML section that the "quotes['KEH15']" refers to (scroll to the right):
</thead>
<tbody>
<script language="javascript">
writeBidRow('Wheat',-60,false,false,false,0.5,'01/15/2015','02/26/2015','All',' ',' ',60,'even','c=2246&l=3519&d=G15',quotes['KEH15'], 0-0);
writeBidRow('Wheat',-65,false,false,false,0.5,'07/01/2015','07/31/2015','All',' ',' ',60,'odd','c=2246&l=3519&d=N15',quotes['KEN15'], 0-0);
</script>
I need to get the value that is represents the "quotes['KEH15']" slot of the html into the string called price. When I run the program, my txt view changes from the default string into a blank. So I think the code is working, but the text view is being updated with a blank string. Can anyone please help me fix this problem?
Thank you for your help.
Keith
Upvotes: 0
Views: 1512
Reputation: 17745
As @njzk2 mentioned you need a javascript engine to do that. Let me elaborate (since you are a beginner I'm going to keep it painfully detailed here). Jsoup is just a parser. What this means is
As it was mentioned earlier Jsoup is just a parser. It retrieves information, nothing more. Which means it can't execute code to produce new HTML pieces. Here is an experiment. Visit a url (facebook, gmail, stackoverflow, whatever works for you, but you are certain that has a lot of js behind it). When you are in that page press Ctrl+U with Chrome. It will open a new tab. This tab shows you exactly what HTML was received from the server, before any javascript was executed and produced new HTML (like the notifications you get on facebook when you have a message). Now go back to the page and press F12 instead. It will open the development tools. Here you are going to see something different. This is the actual HTML rendered by the browser. When you are using Jsoup, then what your program has available is the first HTML, the one before any javascript is executed and that's because Jsoup can't execute javascript, because is just a parser. It's not a browser. A browser can render the additional content, because it can execute javascript code, because it has a javascript engine.
There are two options for you.
Upvotes: 2