获得poi图片的一种方法(百度地图api)

百度地图api中关于poi检索的内容实际上是比较少的。


如果想做poi的介绍界面,需要 poi的图片。。。但是百度地图api中只提供了poi的url。没办法,用正则表达式自己搞!

一开始遇到的问题是无法解析getDetailUrl()的源码,后来发现这个url是个废弃的有么有搞错

自己构建如下"http://map.baidu.com/detail?qt=ninf&uid="+uid.get(i)+"&detail="+type.get(i)"

对于cater类型的网页,获取图片的正则表达式为

"(?<=<img src=\").*(?=\" class=\"head-img\"/>)"  

关键源码如下:
public class webContent {  
    public  byte[] readStream(InputStream inputStream) throws Exception {  
        byte[] buffer = new byte[1024];  
        int len = -1;  
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
  
        while ((len = inputStream.read(buffer)) != -1) {  
        byteArrayOutputStream.write(buffer, 0, len);  
        }  
  
        inputStream.close();  
        byteArrayOutputStream.close();  
        return byteArrayOutputStream.toByteArray();  
        }  
  
        public  String testGetHtml(String urlpath) throws Exception {  
        URL url = new URL(urlpath);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setConnectTimeout(6 * 1000);  
      
//   StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());  
//        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());  
        conn.setRequestMethod("GET");  
  
        if (conn.getResponseCode() == 200) {  
        InputStream inputStream = conn.getInputStream();  
        byte[] data = readStream(inputStream);  
        Log.e("字节数组长度", data.length+"个字节");  
        String html = new String(data);  
        return html;  
        }  
        return null;  
        }  
        public String getTitle(String s) {  
            String regex;  
            String title = "";  
            ArrayList<String> list = new ArrayList<String>();  
            regex = "(?<=<img src=\").*(?=\" class=\"head-img\"/>)";  
            Pattern pa = Pattern.compile(regex);  
            Matcher ma = pa.matcher(s);  
            while (ma.find()) {  
                list.add(ma.group());  
            }  
            for (int i = 0; i < list.size(); i++) {  
                title = title + list.get(i);  
            }  
            return (title);  
        }   public ImageLoader loadImage(String  url,Context context){  
        RequestQueue requestQueue = Volley.newRequestQueue(context);  
        final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(50);  
        ImageCache imageCache = new ImageCache() {  
              
            @Override  
public void putBitmap(String key, Bitmap value) {  
       lruCache.put(key, value);  
}  
  
@Override  
public Bitmap getBitmap(String key) {  
      return lruCache.get(key);  
   }
};  

ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);  
return imageLoader;  
  
}}  


猜你喜欢

转载自blog.csdn.net/tectrol/article/details/78763393