如何从json文件中获取图像的url

时间:2013-07-16 09:19:26

标签: android eclipse android-emulator

我已经编写了这个语句来获取存储在json文件中的图像的URL但是返回的数据是null并且我的模拟器已经崩溃了!!

String url=ent.getJSONObject(i).getJSONArray("media$thumbnail").getJSONObject(0).getString("url").toString();

the object that contains the image url

这个语句的语法是否有任何逻辑错误?

2 个答案:

答案 0 :(得分:2)

使用Jsoup以最简单的方式解决这个问题

http://www.jsoup.org/cookbook/introduction/parsing-a-document/

答案 1 :(得分:1)

使用此代码

   ArrayList<HashMap<String, String>> searchList = new ArrayList<HashMap<String, String>>();
  XMLParser parser = new XMLParser();
  String xml = parser.getXmlFromUrl(URL); // getting XML from URL
  Document doc = parser.getDomElement(xml); // getting DOM element
  NodeList nl = doc.getElementsByTagName(KEY_PRODUCT);
  for (int i = 0; i < nl.getLength(); i++) {
  Cursor cousor_image = null;
  HashMap<String, String> map = new HashMap<String, String>();
  Element e = (Element) nl.item(i);
      map.put(KEY_PRODUCT_ID, parser.getValue(e, KEY_PRODUCT_ID));
      searchList.add(map);
      Element elm = (Element) n3.item(0);

这是代码定义im XMLparser类

    DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
        Log.i("XML Parser", "xml=" + xml);

下面的代码将有助于将位图收集到图像

   public void DisplayImage(String url, ImageView imageView) {
    imageViews.put(imageView, url);
    Bitmap bitmap = memoryCache.get(url);
    if (bitmap != null){
        imageView.setImageBitmap(bitmap);

    }

    else {
        queuePhoto(url, imageView);
        imageView.setImageResource(stub_id);
    }
}

最后是memorycache类

     import java.lang.ref.SoftReference;
     import java.util.Collections;
     import java.util.HashMap;
     import java.util.Map;
     import android.graphics.Bitmap;

     public class MemoryCache {
private Map<String, SoftReference<Bitmap>> cache = Collections
        .synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());

public Bitmap get(String id) {
    if (!cache.containsKey(id))
        return null;
    SoftReference<Bitmap> ref = cache.get(id);
    return ref.get();
}

public void put(String id, Bitmap bitmap) {
    cache.put(id, new SoftReference<Bitmap>(bitmap));
}

public void clear() {
    cache.clear();
}

}