android 利用Jsoup抓取网站数据 报错 求大神指导~
package com.example.service; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import android.net.Uri; import com.example.entity.Goods; public class GoodsService { /** * 获取优来网商城信息 * @return * @throws IOException */ public static List<Goods> getGoods() throws IOException { // TODO Auto-generated method stub String url="http://www.ulai8.com/index.php?do=goodslist"; List<Goods> date =new ArrayList<Goods>(); Goods goods=null; Document doc=Jsoup.connect(url).timeout(5000).get(); Document content=Jsoup.parse(doc.toString()); System.out.println("c"+content); Elements div=content.select("div.list-img"); //System.out.println("a"+div.toString()); Document divcontions =Jsoup.parse(div.toString()); //获取divs中的所有li标记 Elements elements=divcontions.select("li.list-item"); for(Element e:elements) { goods.name =e.select("a.list-title").text(); System.out.println("c"+goods.name); goods.image="http://www.ulai8.com/data/uploads/2014/12/03/210_1196045436547ef8e88b412.jpg"; date.add(goods); } System.out.println("c"+date.get(1)); return date; } /** * 获取网络图片如果图片在缓存中 直接在缓存读取图片 * @param path * @return * @throws IOException * @throws MalformedURLException */ public static Uri getimage(String path,File cacheDir) throws MalformedURLException, IOException { File localFile=new File(cacheDir, "http://www.ulai8.com/data/uploads/2014/12/03/210_1196045436547ef8e88b412.jpg"); if(localFile.exists()) { return Uri.fromFile(localFile); }else { HttpURLConnection conn =(HttpURLConnection) new URL(path).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode()==200) { FileOutputStream outStream= new FileOutputStream(localFile); InputStream inputStream=conn.getInputStream(); byte[] buffer=new byte[1024]; int len =0; while((len=inputStream.read(buffer))!=-1) { outStream.write(buffer, 0, len); } inputStream.close(); outStream.close(); return Uri.fromFile(localFile); } } return null; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Document doc=Jsoup.connect(url).timeout(5000).get();
开始在这一段报错 后面没改什么代码 又换地方报错了 ,我也是醉了 求大神
不能在主线程访问网络。。