0

I've written this statement to get the url of image which is stored in a json file BUT the returned data is null and my emulator has crashed !!

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

the object that contains the image url

is there any logical error in the syntax of this statement ?

7
  • do u mean the image url ? Commented Jul 16, 2013 at 9:21
  • nope, the way you retrieve the Strinh url Commented Jul 16, 2013 at 9:22
  • yes I've tried to retrieve name :String authorName=ent.getJSONObject(i).getJSONArray("author").getJSONObject(0).getJSONObject("name").getString("$t"); ::: and I've succeeded Commented Jul 16, 2013 at 9:24
  • this is what I want to know ???? :( :( Commented Jul 16, 2013 at 9:25
  • 1
    do the following thing: Instead of concat all the object, cut the ent.getJSONObject(i).getJSONArray("media$thumbnail").getJSONObject(0).getString("url").toString(); in pieces, in order to understand what is null Commented Jul 16, 2013 at 9:26

2 Answers 2

2

Use Jsoup to solve this problem in easiest

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

Sign up to request clarification or add additional context in comments.

Comments

1

Use this code

   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);

This is code define im XMLparser classes

    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);

Below code will help to collect the bitmap to image

   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);
    }
}

And finally memorycache class

     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();
}

}

1 Comment

op wants to parse the json file to get the url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.