我正在尝试获取一个img并使用Picasso将其加载到ImageView,但它返回一个白色屏幕,好像它是NULL。我包含了相关的类,还包括img的HTML部分。我想我可能会错误地使用jsoup语法。有什么想法吗?
主:
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)" />
答案 0 :(得分:0)
您的CSS查询应该有所不同:
Elements x = doc.select("div.left img");
旧版本(div.left
)检索到div
代码,而不是img
。