Aziz
Aziz

Reputation: 7

image not loading using json from url

Here is my code. The image space remaining empty. Not being loaded. What is my mistake here?
what kind of code i need again. give more definition pls.

public class MainActivity extends AppCompatActivity {

        private String TAG = MainActivity.class.getSimpleName();

        private ProgressDialog progressDialog;
        private ListView listView;

        // JSON data url
        private static String Jsonurl = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
        ArrayList<HashMap<String, String>> contactJsonList;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            contactJsonList = new ArrayList<>();
            listView = (ListView) findViewById(R.id.listview);
            new GetContacts().execute();
        }

        private class GetContacts extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Please wait...");
                progressDialog.setCancelable(false);
                progressDialog.show();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                HTTPHandler httpHandler = new HTTPHandler();

                // request to json data url and getting response
                String jsonString = httpHandler.makeServiceCall(Jsonurl);
                Log.e(TAG, "Response from url: " + jsonString);
                if (jsonString != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(jsonString);
                        // Getting JSON Array node
                        JSONArray contacts = jsonObject.getJSONArray("actors");

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

                            JSONObject c = contacts.getJSONObject(i);
                            String name = c.getString("name");
                            String country = c.getString("country");
                            String spouse = c.getString("spouse");
                            String dob = c.getString("dob");
                            String description = c.getString("description");
                            String children = c.getString("children");
                            String image = c.getString("image");

                            // tmp hash map for single contact
                            HashMap<String, String> contact = new HashMap<>();

                            // adding each child node to HashMap key => value
                            contact.put("name", name);
                            contact.put("country", country);
                            contact.put("spouse", spouse);
                            contact.put("dob", dob);
                            contact.put("description", description);
                            contact.put("children", children);
                            contact.put("image", image);

                            // adding contact to contact list
                            contactJsonList.add(contact);
                        }
                    } catch (final JSONException e) {
                        Log.e(TAG, "Json parsing error: " + e.getMessage());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                } else {
                    Log.e(TAG, "Could not get json from server.");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Could not get json from server.",Toast.LENGTH_LONG).show();
                        }
                    });
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (progressDialog.isShowing())
                    progressDialog.dismiss();

                /**     * Updating parsed JSON data into ListView    * */
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactJsonList, R.layout.row,
                        new String[]{"name","country", "spouse", "dob", "description", "children", "image"},
                        new int[]{R.id.name, R.id.country, R.id.spouse, R.id.dob, R.id.description, R.id.children, R.id.imageview});

                listView.setAdapter(adapter);
            }
        }
    }

thanks for your help

Upvotes: 0

Views: 407

Answers (3)

Ahsan Iqbal
Ahsan Iqbal

Reputation: 424

Do you want to load list of images from url? then check out the link below, there is detailed example working with list of images using json with volly library. Example

I hope this will help you.

Upvotes: 0

Dhanumjay
Dhanumjay

Reputation: 525

If you want to load image from URL Use custom adapter and use picasso or Glide library to load image. or If you want to use simpleAdapter then check this link Image from URL in ListView using SimpleAdapter

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69691

you can user Glide library to load image from url look the below code it can help you in simple way

compile this library

compile 'com.github.bumptech.glide:glide:4.0.0-RC0'

than load image like this

Glide.with(HomeClass.this)
            .load(userProfileUrl)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .dontAnimate()
            .into(imageview);

Upvotes: 0

Related Questions