简体   繁体   中英

Using Jsoup to parse HTML?

I'm trying to get an img and load it to an ImageView using Picasso but it returns a white screen as if it was NULL. I included the classes that are relevant and also included the HTML part of the img. I think i might be getting the jsoup syntax wrong. any thoughts?

Main:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener   {

    ListView list;
    contentExcluding cont = new contentExcluding();


    listClass adapter;
    String name;

    String[] web = {"Egyt"};
    String[] imageId = {"a"};

    Handler handler;
    Button button;
    contentExcluding con = new contentExcluding();
    String title;
    Document doc;
    Element Current;
    TextView text;
    String x;
    RunnableExample runa = new RunnableExample();
    ImageView image;
    contentExcluding org = new contentExcluding();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        adapter = new listClass(MainActivity.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        image = (ImageView)findViewById(R.id.imageView);
        Picasso.with(this).load(org.str).resize(100,100).centerCrop().into(image);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
            }
        });
    }

contentExcluding;

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

import java.io.IOException;


public class contentExcluding {


    Elements title;
    String str;

    public String kelma() {
        Runnable rMain = new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect("http://egyptianstreets.com/").get();
                    //title = doc.getElementsByClass("left relative>img");
                    //String x = title.attr("src");
                    //str = x;
                    Elements x = doc.select("div.left");
                    str = x.attr("src");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };
        Thread t = new Thread(rMain);
        t.start();

        return str;
    }
}

HTML;

<div id="head-wrap" class="left relative">
        <div class="head-wrap-out">
            <div class="head-wrap-in">
                                <div id="featured-multi-wrap" class="left relative">
                                    <div id="featured-multi-main" class="left relative">
                        <a href="http://egyptianstreets.com/2016/08/06/egyptian-rowers-nadia-negm-and-abdel-khalek-elbana-reach-quarter-finals-at-olympics/" rel="bookmark">
                        <div id="featured-multi-main-img" class="left relative">
                                                            <img width="1000" height="512" src="http://egyptianstreets.com/wp-content/uploads/2016/08/pablo-2-1000x512.png" class="unlazy reg-img wp-post-image" alt="pablo (2)" />                             <img width="400" height="240" src="http://egyptianstreets.com/wp-content/uploads/2016/08/pablo-2-400x240.png" class="unlazy mob-img wp-post-image" alt="pablo (2)" />   

Your CSS query should be different:

Elements x = doc.select("div.left img");

The old one ( div.left ) retrieved the div tag and not the img .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM