如何用jsoup解析图像 [英] How parse image with jsoup
问题描述
我找不到任何解决方案!!我需要用 jsoup 解析一个 html 页面,我也需要解析图像,但我做不到!这是我的 MainActivity
I can't find any solution for this!! I need parsing a html page with jsoup and i need parse the image too but i can't do it! This is my MainActivity
public class MainActivity extends Activity {
public static final String TAG_TITOLI = "titoli";
private static final String TAG_CONTENT = "content";
ListView lista;
static final String BLOG_URL = "http://www.multiplayer.it";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (ListView)this.findViewById(R.id.main_lista);//recupero lista da id
//creo ed eseguo l'asynctask
ParsingPaginaWeb parsing = new ParsingPaginaWeb();
parsing.execute("");
// Launching new screen on Selecting Single ListItem
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String titoli = ((TextView) view.findViewById(R.id.riga_listview_titolo)).getText().toString();
//String cont = ((TextView) view.findViewById(R.id.riga_descrizione)).getText().toString();
//String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleActivity.class);
in.putExtra(TAG_TITOLI, titoli);
//in.putExtra(TAG_CONTENT, cont);
//in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
}
});
}
private class ParsingPaginaWeb extends AsyncTask<String,String,String> {
ArrayList<String> titoli; //lista dei titoli
//ArrayList<String> content; //lista delle descrizioni
@Override
protected void onPreExecute()
{
Toast.makeText(MainActivity.this ,"Caricamento lista titoli...", Toast.LENGTH_SHORT).show();
//prima di eseguire il parsing inizializzo gli arraylist
titoli = new ArrayList<String>();
//content = new ArrayList<String>();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(BLOG_URL).get();
Elements nodeBlogStats = doc.select("div.news-col-0 h3"); //per multiplayer.it Elements nodeBlogStats = doc.select("div.news-col-0 h3"); per ftv #comunePartINI > option
for(Element sezione : nodeBlogStats)
{
titoli.add(sezione.text());
}
} catch (Exception e) {
// In caso di errore
Log.e("ESEMPIO", "ERRORE NEL PARSING");
}
return null;
}
@Override
protected void onPostExecute(String result)
{
// dopo che ho eseguito il parsing mostro i dati nella listview
// usando il custom array adpater ParsingArrayAdapter
ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli);
lista.setAdapter(adapter);
}
}
}
我可以在其中解析标题并在 ListView
中显示它们.这是适配器:
In which i can parse the titles and dispaly them in a ListView
.. This is the Adapter:
public class ParsingArrayAdapter extends ArrayAdapter<String>{
//riferimenti statici alle risorse e agli id
private final static int LAYOUT = R.layout.riga_listview;
private final static int TITOLO = R.id.riga_listview_titolo;
//private final static int DESCRIZIONE = R.id.riga_listview_descrizione;
ArrayList<String> titoli; //lista dei titoli
Context c; //context
LayoutInflater inflater; //layout inflater
public ParsingArrayAdapter(Context context,ArrayList<String> titoli)
{
super(context,TITOLO);
this.c = context;
this.titoli = titoli;
this.inflater = LayoutInflater.from(c);
}
@Override
public int getCount()
{
return titoli.size(); //ritorno lunghezza lista ( = numero dei titoli)
}
//quando la lista richiede una view
@Override
public View getView(int pos,View view,ViewGroup parent)
{
CacheRiga cache; //cache
if(view==null)//se è la prima volta che viene richiesta la view
{
// creo la view ma non l'attacco alla lista in quanto devo ancora modificare
// i testi delle textview
view = inflater.inflate(LAYOUT, parent,false);
cache = new CacheRiga(); //inizializzo la cache
cache.titolo = (TextView) view.findViewById(TITOLO); //collego titolo
//cache.descrizione = (TextView) view.findViewById(DESCRIZIONE);//collego descrizione
view.setTag(cache);//collego view con cache
}
else
{
cache = (CacheRiga) view.getTag(); //altrimenti prendo la cache dalla view
}
cache.titolo.setText(titoli.get(pos)); //imposto il titolo
return view;
}
private class CacheRiga { // classe per la cache delle righe
public TextView titolo; // cache titolo
//public TextView descrizione; // cache descrizione
}
}
如何为每篇文章插入图片?我不能简单地解析图像的 div
因为我显示一串字符..有没有办法将它插入到列表视图中?谢谢.啊,如果需要,这是 riga_listview.xml
,我想,我需要在其中插入 ImageView
部分.
How can i insert the images for each article? I can't simply parse the div
of image because i display a string of characters.. Is there a way to insert it in the listview? Thanks. Ah if needs, this is the riga_listview.xml
in which, i suppose, i need insert the ImageView
part.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/riga_listview_titolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:layout_margin="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
推荐答案
分解 -
- 从div标签中获取图片的url
- 在列表视图中显示该图像
现在对于1",可以使用jsoup解析图片的url
Now for "1", you can use jsoup and parse the url of the image
对于2",首先将图像视图添加到您的列表视图 xml然后创建一个异步任务,将图像 url 和图像视图的引用传递给它,并在 onPostExecute 中设置它的值.你说网站是动态设置图片的,那么每次在列表视图适配器的get view方法上,你都必须解析网页.虽然这听起来非常低效和密集,但我认为没有其他方法.如果您能以某种方式知道图像何时会发生变化,那么您就可以对该事件进行处理.无论如何,这是从 url 下载图像并将其设置为图像视图的 asynctask 的代码
For "2", first add image view to your list view xml Then create an asynctask, pass image url to it and reference of image view and set its value in onPostExecute. You said that website sets image dynamically, in that case every time on the get view method of list view adapter, you have to parse the webpage. Although this sounds very inefficient and intensive, but I don't think there is any other way. If somehow you can know when is the image going to change, then you can do this processing on that event. Anyway, this is the code of the asynctask to download the image from a url and set it to image view
public class ThumbnailDownloader extends AsyncTask<String, Integer, Bitmap>{
ImageView imview;
Context ctx;
public ThumbnailDownloader(Context c, ImageView imview){
this.imview = imview;
this.ctx = c;
}
@Override
protected Bitmap doInBackground(String... urls) {
try{
HttpURLConnection connection = (HttpURLConnection)new URL(getThumbUrl(urls[0])).openConnection();
connection.connect();
InputStream input= connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
}
catch (Exception f) {
// TODO Auto-generated catch block
f.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result != null && imview!=null){
imview.setImageBitmap(result);
}
}
public String getThumbUrl(String videoUrl){
return "http://img.youtube.com/vi/"+videoUrl+"/default.jpg";
}
}
getThumbUrl 方法必须根据需要修改.上面指定的那个用于下载 youtube 视频缩略图.
The getThumbUrl method you have to modify according to your need. The one specified above is used to download youtube video thumbnails.
这篇关于如何用jsoup解析图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!