0

I am using JSON parser to parse JSON Response, where I am able to get item names in ListView but I am not able to get images corresponding to it. Where image url's are present in JSON response. How can I parse response to get images corresponding to items.

My code..

jsonParser.java

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

}

Then in the main class ..............................

JSONParser jParser1 = new JSONParser();
  json1 = jParser1.getJSONFromUrl(url0);

    try {
        Log.d("Parsing JSON Data", "Before json1 try2");
        activities = json1.getJSONArray(TAG_ACTIVITIES);
        Log.d("Parsing JSON Data", "after activiti");

        for (int i = 0; i < activities.length(); i++) {
            JSONObject c = activities.getJSONObject(i);
            Log.d("Parsing JSON Data", "inside for loop");
            String time = c.getString("time");

            user_name = c.getString("user_name");
            // Image_url=c.getString("thumb");
            // BitmapFactory.Options bmOptions;
            // bmOptions = new BitmapFactory.Options();
            // bmOptions.inSampleSize = 1;
            // Bitmap bm = LoadImage(Image_url, bmOptions);

            String utype = c.getString("type");

            HashMap<String, Object> temp11 = new HashMap<String, Object>();
            Log.d("Parsing JSON Data", "inside if");
            if (utype.contains("user")) {
                temp11.put(TAG_USER_NAME, user_name + " " + join);

            } else if (utype.contains("checkin")) {
                subscriber_name = c.getString("subscriber_name");
                temp11.put(TAG_USER_NAME, user_name + " " + checkin1 + " "
                        + subscriber_name);

            } else {
                subscriber_name = c.getString("subscriber_name");
                temp11.put(TAG_USER_NAME, user_name + " " + favorite1 + " "
                        + subscriber_name);
                // temp11.put("subscriber_name", subscriber_name);
            }
            temp11.put(TAG_TIME, time);
            temp11.put("image1", bm);

            list13.add(temp11);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

}
Aminesrine
  • 2,082
  • 6
  • 35
  • 64
Varun Nayyar
  • 887
  • 4
  • 20
  • 46

3 Answers3

0

Use any Image uploader and pass image url in demo

1) Nostra's Universal Image Loader.

2) Fedor's LazyList. And;

3) Novoda's ImageLoader.

Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

You can use Android-Query (AQuery) or Smart image view .This will help you to load image from json easily

Aminesrine
  • 2,082
  • 6
  • 35
  • 64
Arunkumar
  • 103
  • 1
  • 12
0

Try URLImageViewHelper->

Refer this

It does caching & async loading of images by itself.

Vishal Pawale
  • 3,416
  • 3
  • 28
  • 32