Thursday, 12 September 2013

Scraped JSOUP Data Causes Null Value

Scraped JSOUP Data Causes Null Value

I'm getting a null value for "title" located here:
textView.setText(title);
Which is supposed to update my textview. I've done a bit of debugging and
it appears doc in the line:
title = doc.title();
contains the data for the title tag I'm looking for:
<!DOCTYPE html>
<html itemscope="" itemtype="http://schema.org/WebPage">
<head>
<meta content="Search the world's information, including webpages,
images, videos and more. Google has many special features to help you
find exactly what you're looking for." name="description" />
<meta content="noodp" name="robots" />
<meta itemprop="image" content="/images/google_favicon_128.png" />
<title>Google</title>
but the value of the title tag (google.com) is never passed to the
textview and I'm not sure why.
Any input is greatly appreciated.
SOURCE:
package com.example.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private String response;
String title;
public interface Callback {
void onModifiedTextView(String value);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.TextView01);
textView.setVisibility(View.VISIBLE);
}
public void onModifiedTextView(final String title) {
runOnUiThread(new Runnable() {
public void run() {
textView.setText(title);
textView.invalidate(); // not even necessary
}
});
}
public class DownloadWebPageTask extends AsyncTask<String, Void,
String> {
public DownloadWebPageTask(MainActivity mainActivity) {
this.callback = mainActivity;
}
private MainActivity callback;
public DownloadWebPageTask() {
// TODO Auto-generated constructor stub
}
public DownloadWebPageTask(TextView textView) {
// TODO Auto-generated constructor stub
}
@Override
protected String doInBackground(String... urls) {
//String response = title;
for (String url : urls) {
try {
Document doc = Jsoup.connect("http://google.com")
.userAgent("Mozilla")
.get();
// get page title
title = doc.title();
System.out.println("title : " + title);
// get all links
Elements links = doc.select("a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(final String title) {
textView.setText(title);
}
}
public void onClick(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.google.com" });
}
}

No comments:

Post a Comment