0

Here, I've to get image from json data and have to set that image in image view. I have got all the other necessary data from json and set them to text view, but could not set the image to image view. Below is my code,

MainActivity.java

public class MainActivity extends ListActivity {
    private Context context;

    // this url returns json encoded data
    private static String url = "http://192.168.0.104/productdetails/product.php";

    private static final String NAME = "name";
    private static final String STORE_NAME = "store_name";
    private static final String PIC = "pic";
    //private static final String TREAD = "Tread";

    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

    ListView lv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ProgressTask(MainActivity.this).execute();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;

        private ListActivity activity;

        // private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Please wait...");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                    R.layout.list_item, new String[] { NAME, STORE_NAME, PIC,
                             }, new int[] {
                            R.id.vehicleType, R.id.vehicleColor, R.id.image });

            setListAdapter(adapter);
            lv = getListView();
        }

        protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();

            // get JSON data from URL
            JSONArray json = jParser.getJSONFromUrl(url);

            for (int i = 0; i < json.length(); i++) {

                try 
                {
                    JSONObject c = json.getJSONObject(i);
                    String name = c.getString(NAME);

                    String storename = c.getString(STORE_NAME);
                    String pic = c.getString(PIC);


                    HashMap<String, String> map = new HashMap<String, String>();

                    // Add child node to HashMap key & value
                    map.put(NAME, name);
                    map.put(STORE_NAME, storename);
                    map.put(PIC, pic);

                    jsonlist.add(map);
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
}

JSONParser.java

public class JSONParser {

    static InputStream iStream = null;
    static JSONArray jarray = null;
    static String json = "";

    public JSONParser() {
    }

    public JSONArray getJSONFromUrl(String url)
    {

        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        try 
        {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if (statusCode == 200) 
            {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) 
                {
                    builder.append(line);
                }
            } 

            else 
            {
                Log.e("==>", "Failed to download file");
            }
        }

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

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

        // Parse String to JSON object
        try
        {
            jarray = new JSONArray(builder.toString());
        } 

        catch (JSONException e) 
        {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON Object
        return jarray;

    }
}

How to decode image and show in image view?

thumber nirmal
  • 1,639
  • 3
  • 16
  • 27

0 Answers0