How to use Jsoup library to get "a" tag from a webpage?

 

Step 1: Go to: http://jsoup.org/download and download core library to your desktop or any location you want. Ctrl + C to make a copy of your .JAR library file.

Step 2: Here i use Android studio, see on the top-left side of android studio, change “android” to “project” explorer view. Browse to the path: /App/libs, right click on “libs” folder and paste the .JAR file we copied before.

Step 3: Right click on your project name in project explorer, open module settings, a new dialog appear. See on the top left and click add button (a green plus icon). choose “import .JAR….blabla”. Next, browse to the jAR file and click finish. Waiting for the gradle is built successfully.

See the code below:


package com.wordpress.davidng.spinner;

import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private TextView tv;
private Spinner spinner;

ArrayList<String> links = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
spinner = (Spinner) findViewById(R.id.spn);

runOnUiThread(new Runnable() {
@Override
public void run() {
new readJsoup().execute("http://daynhauhoc.com");
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tv.setText("Bạn đã chọn: " + links.get(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

class readJsoup extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Elements link = doc.select("a");
for(Element a : link){
links.add(a.attr("abs:href").toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, links);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinner.setAdapter(adapter);
}
}

}

Note: Remember to modify android manifest and add INTERNET permission.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s