Android JSoup not working - android

I want to extract information from the web and show that value in my Android app. When I try to write the following code, nothing gets initialized to my textView. I can't see the data I wanted. Can you please tell me whats wrong?
EDIT: Android is now not even going past the line:
Document doc = Jsoup.connect("http://movies.ign.com/articles/100/1002569p1.html").get();
When I run the emulator, it just exits the App. Why is this happening??
Here is my code:
public class Search extends Activity {
private static final String TAG = "TVGuide";
String outputtext;
Parser parser;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
TextView outputTextView = (TextView) findViewById(R.id.outputTextView);
String id = "main-article-content";
try {
Document doc = Jsoup.connect("http://movies.ign.com/articles/100/1002569p1.html").get();
Elements elementsHtml = doc.getElementsByAttributeValue("id", "main-article-content");
for (Element element : elementsHtml) {
Log.i("PARSED ELEMENTS:", URLDecoder.decode(element.text(), HTTP.UTF_8));
outputTextView.setText(element.text());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

I think you imported the class org.w3c.dom.Document instead of the required one, org.jsoup.nodes.Document by mistake.

EDIT: Android is now not even going past the line:
Jsoup cant connect to the site? Try to add timeout on the connect:
Document doc = Jsoup.connect("http://movies.ign.com/articles/100/1002569p1.html").timeout(10000).get();

Related

Android Jsoup find specific element

I wish to parse IMBD page to get the movie rating. (Please do not offer me APIs). This is my code (for now):
private static class getData extends AsyncTask<String, String, Void>
{
String url = "https://www.imdb.com/title/tt0437086/";
#Override
protected Void doInBackground(String... strings) {
try {
Document document = Jsoup.connect(url).get();
Elements img = document.select("span");
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
I get all the span, but do I need to cycle all of them to find the rating?
What I need is the rating from this line (specifically the rating itself):
<span itemprop="ratingValue">7.5</span>
How can I get the rating without cycling trough all elements?
You can select a specific span
Elements img = document.select("span[itemprop=ratingValue]");
Log.e("TEST", "Result: " + img.text());
I tested here and it is properly printing 7.5
You can find more info about the selector syntax here

Retrieving information from google page

I'm thinking about making my first android app, It'd be about movies, I found an excellent data source, it is "http://www.google.com/movies?" but I wanted to know how could I extract this information and put it in my app,
I've searched but I don't know which is the optimal way to do this? does google have an API for this? is that what I want? is it better with the source code?what could I read or see to learn to do this?
thanks a lot guys, Is my first time as well programming retrieving information from the cloud,
cheers
Yup. Here is one way to do it.
First, you need to find the source of the SQL. The Yahoo Developer Console is a great place to look for this sort of stuff. It has EVERYTHING. The way these resources work is that you have a long link, like this....
developer.yahoo.com/blah/this . . . &q=KEYWORD_HERE+blah/ . . .
To access the information you are looking for, you stick whatever the correct keyword is where "KEYWORD_HERE" is, and the link will give you info in SQL format. I'll be doing the example as a stocks app.
First you create an Activity and define both sides of your link as strings. It'll look a bit like this:
public class InfoActivity extends Activity {
String firstHalf = "http://query.yahooapis.com/v1/public/blahblahblah&q=";
String secondHalf = "+blah/blah&blah . . . ";
Then, in your onCreate, you'll need to start an aSync task to do the actual pulling and parsing:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.id.layout_name);
final String yqlURL = firstHalf + KEYWORD_HERE + secondHalf;
new MyAsyncTask().execute(yqlURL);
}
Then to define our MrAsyncTask:
private class MyAsyncTask extends AsyncTask<String, String, String>{
protected String doInBackground(String... args) {
try {
URL url = new URL(args[0]);
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
// Tests if responseCode == 200 Good Connection
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("nodeName1");
if (nl != null && nl.getLength() > 0) {
for (int i = 0 ; i < nl.getLength(); i++) {
//Parse the node here with getTextValue(n1, "Name of element")
//ex: String movieName = getTextValue(n1, "MovieName");
}
}
}
} catch (MalformedURLException e) {
Log.d(TAG, "MalformedURLException", e);
} catch (IOException e) {
Log.d(TAG, "IOException", e);
} catch (ParserConfigurationException e) {
Log.d(TAG, "Parser Configuration Exception", e);
} catch (SAXException e) {
Log.d(TAG, "SAX Exception", e);
}
finally {
}
return null;
}
I hope that gives you some idea of how to do this sort of thing. I'll go see if I can quickly spot a good resource on the yahoo apis to get the movie times at a certain location.
Good luck :) Let me know if you need anything clarified.
EDIT:
Looks like this is EXACTLY what you need (resource wise):
https://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys#h=select+*+from+google.igoogle.movies+where+movies%3D'68105'%3B
Check that out. Using that, your two halves of the link would be:
String firstHalf = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.igoogle.movies%20where%20movies%3D'"
String secondHalf = "'%3B&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
And then to get your final link, you would just do
String yqlURL = firstHalf + "ZIP CODE OF YOUR LOCATION" + secondHalf;
And you would have all of the movies playing near you returned!
Make your life a lot easier and choose the api that is right for you. Choose one of these:
http://www.programmableweb.com/news/52-movies-apis-rovi-rotten-tomatoes-and-internet-video-archive/2013/01/22
Make your decision not only based on the content, but also ease of use and documentation. Documentation is a biggy.
Good luck!
well i would rather advice you to use an TheMovieDB.com API it is simple and provides every info of movies.

String.contains doing opposite Jsoup Android

I may be misunderstanding what String.contains does. I am now trying to pull a specific link using Jsoup in Android. I'm trying to just get the faceBook one as an example. Ive tried a few things. this one It Seems to be outputting got it on the ones that do not contain the facebook url and leaving the facebook ones blank. How do I just get the FaceBook ones and stop the loop.
protected Void doInBackground(Void... params) {
Document doc = null;
try {
doc = Jsoup.connect("http://www.homedepot.com").get();
Elements link = doc.select("a[href]");
String stringLink = null;
for (int i = 0; i < link.size(); i++) {
stringLink = link.toString();
if (stringLink.contains("https://www.facebook.com/")){
System.out.println(stringLink+"got it");
}
else{
//System.out.println(stringLink+"not it");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
The following line is causing the problem:
stringLink = link.toString();
The link variable is a collection of Elements (in this case every link on the page), so by calling link.toString() you're getting the String representation of every single link on the page all at once! That means stringLink will always contain the facebook link!
Change the line to:
stringLink = link.get(i).toString();
This line gets only the link at index i on each iteration and checks whether or not it contains the facebook link.

ImageView works on android min sdk 2.2 but not when I changed to 3.0?

I'm making an android application that uses internet data. I developed on min sdk version 2.2 but now I changed it to 3.0. My problem is that now some of the components don't work. My guess is that some code in version 2.2 might be deprecated in 3.0. But I have no idea what to do to make it work.
For example, my ImageView is working in 2.2. But in 3.0 its not.
//******satellite url
private void satellite() {
// TODO Auto-generated method stub
ImageView imgView =(ImageView)findViewById(R.id.satellite);
Drawable drawable = LoadImageFromWeb("http://www.pagasa.dost.gov.ph/wb/sat_images/satellite.gif");
imgView.setImageDrawable(drawable);
}
Another is my json parser. It also work perfectly in 2.2 but not in 3.0
//json stuffs , setText on textViews in main menu
private void jsonStuffs() {
// TODO Auto-generated method stub
//JSON PARSER & HOME PAGE TEXTVIEWS
client = new DefaultHttpClient();
//new Read().execute("id");
//http client codes only no parser !!
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
String jsonStr = test.getInternetData(); //punta sa GetMethodEx
JSONObject obj = new JSONObject(jsonStr);
//find temperature sa JSON sa webpage
String temperature = obj.getString("temperature");
TextView tvTemp = (TextView)findViewById(R.id.temp);
tvTemp.setText(temperature);
//find sunrise sa JSON sa webpage
String sunrise = obj.getString("sunrise");
TextView tvSunrise = (TextView)findViewById(R.id.sunrise);
tvSunrise.setText("Sunrise: " + sunrise);
//find sunset sa JSON sa webpage
String sunset = obj.getString("sunset");
TextView tvSunset = (TextView)findViewById(R.id.sunset);
tvSunset.setText("Sunset: " + sunset);
}
//catch (JSONException e) {
// e.printStackTrace();
//}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I haven't received any errors on this project. Just the data is not displaying, the imageview is not displaying etc.
I hope someone can help. Thanks in advance :)
I think the reason is Android 3.0 and above, you need to do network operation like http request and all in seperate thread then the UI thread. You are directly using the code in UI thread so it works below HoneyComb. So you need to put a web request in seperate thread then UI. AsyncTask is a good option.
For more detail check this link.

Android XML XPath error

I have searched the internet quite a lot about this, but I am still struggling to get the following simple code working. Can anybody give me a hand?
The target platform is Android 2.2.
When evaluating, 1 node is returned, but the value is null - I would like the filename.
XML Reading code:
public String GetValue(String data, String xpath) {
String value = "";
XPath path = XPathFactory.newInstance().newXPath();
try {
XPathExpression exp = path.compile(xpath);
Document doc = this.GetDocument(data);
NodeList nodes = (NodeList)exp.evaluate(doc, XPathConstants.NODESET);
if(nodes.getLength() > 0) {
value = nodes.item(0).getNodeValue();
}
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
Calling Code:
XMLHelper xhelp = new XMLHelper();
this.FileName = xhelp.GetValue(fileData, "tilesheet/filename");
The XML:
<?xml version="1.0" encoding="utf-8"?>
<tilesheet>
<filename>sheet.png</filename>
<cellwidth>32</cellwidth>
<cellheight>32</cellheight>
<columns>9</columns>
<cellcount>9</cellcount>
</tilesheet>
Thank you very much for your help,
Richard Hughes
OK. Just looking at this: How to do XPath or XML parsing in Android It seems this works: /tilesheet/filename/text()

Categories

Resources