54383

Cannot Parse HTML Data Using Android / JSOUP

I'm having an issue where I'm attempting to use JSOUP to obtain data from an webpage (in this case - google.com) and when debugging the title data is returned and shown in the logcat - however my textview never seems to update with the freshly obtained data.

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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.TextView01); } private class DownloadWebPageTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { String response = ""; for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { Document doc = Jsoup.connect("http://google.com") .userAgent("Mozilla") .get(); // get page title String 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(String title) { textView.setText(title); } } public void onClick(View view) { DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(new String[] { "http://www.google.com" }); } }

EDIT: (in response to superuser's suggestion - implementing handler)

public class MainActivity extends Activity { private TextView textView; private Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.TextView01); handler = new Handler(); } private class DownloadWebPageTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { String response = ""; for (String url : urls) { DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { Document doc = Jsoup.connect("http://google.com") .userAgent("Mozilla") .get(); // get page title String 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(); } handler.post(new Runnable() { @Override public void run() {}}); } return response; } @Override protected void onPostExecute(String title) { textView.setText(title); View.invalidate(); } } public void onClick(View view) { DownloadWebPageTask task = new DownloadWebPageTask(); task.execute(new String[] { "http://www.google.com" }); } }

RESULTS (from edit shown above):

Cannot make a static reference to the non-static method invalidate() from the type View MainActivity.java Cannot refer to a non-final variable title inside an inner class defined in a different method MainActivity.java

Answer1:

Sorry, was about to answer this question yesterday but fell asleep on my keyboard :P

But your result string: protected void onPostExecute(String result) doesn't get anything passed. The problem is easily solved.

<ol> <li>Above your onCreate:</li> </ol>

String title;

<ol> <li>In your doInBackGround:</li> </ol>

title = doc.title();

<ol> <li>

In your onPostExecute:

@Override protected void onPostExecute(String result) { textView.setText(title); } </li> </ol>

Answer2:

Try passing the textview as an contructor argument to your class DownloadWebPageTask.

DownloadWebPageTask task = new DownloadWebPageTask(textView);

In your DownloadWebPageTask class define a TextView variable to hold this object.

Update the same in onPostExecute() method.

Recommend

  • Android - What should I use to get data from remote db?
  • How to use CompletableFuture without risking a StackOverflowError?
  • Fancy dynamic list in Android: TableLayout vs ListView
  • Why does .addView throw this parent/child exception?
  • How to Handle Click on Imageview in Custom Listview with image and Textview?
  • Picasso Taking time to load images
  • Null ImageView Reference
  • How to change placeholder text in an autocomplete activity of android google place?
  • How to create a new Bundle object?
  • How to upload specific List image using click on Upload button
  • Two Tables Serving as one Model in Rails
  • How to get listview position?
  • JqueryMobile Popup menu is not working
  • Connect .sks to skscene.h
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • How to test if a URL from an Eclipse bundle is a directory?
  • Projection media query: browser support and workarounds?
  • Android screen density dpi vs ppi
  • Deselecting radio buttons while keeping the View Model in synch
  • Deleting and Updating values from a cusrsor adapter
  • Azure Cloud Service Web Role web pages do not load
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Obtain ObjectIdHex value from mgo query
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • C# - Getting references of reference
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How do you join a server to an Active Directory (domain)?
  • JTable with a ScrollPane misbehaving
  • Understanding cpu registers
  • Getting Messege Twice Using IMvxMessenger
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?