gpt4 book ai didi

java - Jsoup 多个连接 - 使用已解析的 URL 元素

转载 作者:行者123 更新时间:2023-12-01 11:53:06 25 4
gpt4 key购买 nike

这是我的 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();
}
}

}

像往常一样,我在 AsyncTask 中使用 jsoup 解析一些数据。现在,我需要解析另一个应该包含元素或 w/e 的 url,例如aRightPostTekst.attr("href")

我可以在同一个异步任务中执行它还是应该创建一个新任务?

基本上我需要我的网址是:

"http://url.com/"+ aRightPostTekst.attr("href")

然后我应该解析一些其他元素并将其放入数组列表中,然后再使用它。

是否可能,例如多个连接并使用一个 url 元素?

我正在从以下网站进行解析:http://gimnazijabihac.edu.ba/,并且我正在解析异步任务中的 href 标记,如下所示:/e-novine/n/?id=340,则完整网址为:http://gimnazijabihac.edu.ba/e-novine/n/?id=340

我应该解析该特定网站的 html。

提前致谢。

最佳答案

你需要单独解析每个 URL,但你应该能够在一个线程中完成它(我不是 Android 开发人员,所以我假设 AsyncTask 与 Thread 类似)。

也可以代替

"http://url.com/" + aRightPostTekst.attr("href") 

你可以使用

aRightPostTekst.attr("abs:href")

使用绝对路径(http://server/context/href)而不是相对路径获取href

关于java - Jsoup 多个连接 - 使用已解析的 URL 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28636284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com