Reputation: 1669
Here's my PocetnaFragment.java:
package gimbi.edu.ba;
import java.util.ArrayList;
import java.util.HashMap;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Fragment;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.nhaarman.listviewanimations.appearance.AnimationAdapter;
import com.nhaarman.listviewanimations.appearance.simple.SwingBottomInAnimationAdapter;
import com.nispok.snackbar.Snackbar;
import com.software.shell.fab.ActionButton;
public class PocetnaFragment extends Fragment {
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String SEKCIJA = "sekcija";
static String NASLOV = "naslov";
static String LINK = "link";
static String SLIKA = "slika";
// URL Address
String url = "http://gimnazijabihac.edu.ba";
private SwipeRefreshLayout swipeView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_pocetna, container, false);
listview = (ListView) view.findViewById(R.id.pocetna_listview);
ActionButton actionButton = (ActionButton) view.findViewById(R.id.action_button);
actionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!isNetworkAvailable(getActivity())) {
Snackbar.with(getActivity()) // context
.text("Neuspjela konekcija.") // text to display
.duration(Snackbar.SnackbarDuration.LENGTH_LONG) // make it shorter
.show(getActivity()); // activity where it is displayed
} else {
new JsoupListView().execute();
}
}
});
swipeView = (SwipeRefreshLayout) view.findViewById(R.id.pocetna_swipe);
swipeView.setColorScheme(android.R.color.holo_blue_dark, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
new JsoupListView().execute();
swipeView.setRefreshing(false);
}
}, 500);
}
});
if(!isNetworkAvailable(getActivity())) {
Snackbar.with(getActivity()) // context
.text("Neuspjela konekcija.") // text to display
.duration(Snackbar.SnackbarDuration.LENGTH_LONG) // make it shorter
.show(getActivity()); // activity where it is displayed
}
new JsoupListView().execute();
return view;
}
public boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
else return false;
} else
return false;
}
// Title AsyncTask
private class JsoupListView extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity(), ProgressDialog.THEME_HOLO_LIGHT);
mProgressDialog.setMessage("Učitavanje..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
arraylist = new ArrayList<HashMap<String, String>>();
Elements aEles = null;
Elements divRightPostEles = null;
String rightNaslov = null;
Document doc = null;
try {
doc = Jsoup.connect(url).get();
/** Get A tag that is under DIV with classname right_naslov **/
aEles = doc.select("div.right_naslov > a");
if (aEles != null && aEles.size() > 0) {
if (aEles.size() == 2)
rightNaslov = aEles.get(1).ownText();
else
rightNaslov = aEles.first().ownText();
}
/**
* Since you say there are multiple DIV with right_post as
* classname, we will get all those right post elements and loop
* them one by one to retrieve its inner elements
**/
divRightPostEles = doc.select("div.right_post");
for (Element rightPostDiv : divRightPostEles) {
/** Each loop of this represents a right_post DIV element **/
HashMap<String, String> map = new HashMap<String, String>();
/**
* Get Font tag with [classname=nadnaslov] under
* span[classname=right_post_nadnaslov] under
* div[lassname=right_post]
**/
/** Try to get Font[classname=naslov] with the following method **/
Elements fontNadnaslov = rightPostDiv
.select("span.right_post_nadnaslov > font.nadnaslov");
/**
* Get A tag that is under div[classname=right_post_tekst] under
* div[classname=right_post]
**/
Element aRightPostTekst = rightPostDiv.select(
"div.right_post_tekst > a[href]").first();
// Retrive Jsoup Elements
if (fontNadnaslov != null && fontNadnaslov.size() > 0) {
map.put("naslov", fontNadnaslov.first().ownText());
if (aRightPostTekst != null) {
map.put("link", aRightPostTekst.attr("href"));
Element img = aRightPostTekst.select("img[src]").first();
if (img != null)
map.put("slika", "http://gimnazijabihac.edu.ba/" + img.attr("src"));
}
if (rightNaslov != null)
map.put("sekcija", rightNaslov);
// Set all extracted Jsoup Elements into the array
arraylist.add(map);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(), arraylist);
AnimationAdapter animationAdapter = new SwingBottomInAnimationAdapter(adapter);
animationAdapter.setAbsListView(listview);
// Set the adapter to the ListView
listview.setAdapter(animationAdapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
As usually, I'm parsing some data with jsoup within an AsyncTask. Now, I need to parse another url that should contain an element or w/e, e.g. aRightPostTekst.attr("href")
.
Could I do it inside the same asynctask or should I make a new one?
Basically I need my url to be:
"http://url.com/" + aRightPostTekst.attr("href")
And then I should parse some other elements and put in the arraylist and then later, use it.
Is it possible, like multiple connections and using an element for an url?
I'm parsing from the following website: http://gimnazijabihac.edu.ba/
, and I'm parsing the href tag inside the async task like lets say: /e-novine/n/?id=340
, then the full url is: http://gimnazijabihac.edu.ba/e-novine/n/?id=340
And I should parse html from that specific website.
Thanks in advance.
Upvotes: 0
Views: 1496
Reputation: 25
You can parse two URLs but you have to create a connection for each URL separately and create a document for each different URL and parse accordingly.
Upvotes: 0
Reputation: 124235
You need to parse each URLs separately, but you should be able to do it in one thread (I am not Android developer so I assume that AsyncTask is similar to Thread).
Also instead of
"http://url.com/" + aRightPostTekst.attr("href")
you can use
aRightPostTekst.attr("abs:href")
to get href
with absolute path (http://server/context/href
) instead of relative one.
Upvotes: 1