title: External Image loads on ListView intermittently
body: <p>I am using Volley trying to populate my ListView with data from an external server. I am retrieiving an image and a String for each line on the ListView.</p>
  
  <p>For testing, all I have is 3 sets of image and String which would appear on the ListView as follows: </p>
  
  <p><a href="https://i.stack.imgur.com/zKNqz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zKNqz.jpg" alt="enter image description here"></a></p>
  
  <p>Problem is this doesn't always load. Sometimes its instant. Otherwise, it never loads even if I wait 5 minutes. Average it loads fine about 1 out of 5 tries. I am using Volley and believe it runs on its own thread thus no interference. I don't see any Exceptions. It just gets stuck and never loads image and text. </p>
  
  <p>I am holding the URL to the location of the images and pointing to that location to obtain the image. For example pointing to this a URL: </p>
  
  <pre><code>https://cdn3.iconfinder.com/data/icons/free-social-icons/67/facebook_circle_color-512.png
  </code></pre>
  
  <p>Passing in the URL on a browser instantly retrieves the image instantly. I have tried to load only the String without the images. (ie: item 1, item 2, item 3). That always works but even then, that takes about 5 seconds.</p>
  
  <p>Am I fetching the information wrong? Is there anyway I can optimise it? Even insanely large applications are fetching data in a couple of seconds. This is plain ridiculous for a test data of 3 images and 3 text. Please advice. </p>
  
  <p>Since I am loading data on the ListView on the fly, I am using a CustomAdapter class as follows: </p>
  
  <p><strong>CustomProductListAdapter class</strong></p>
  
  <pre><code>public class CustomProductListAdapter extends BaseAdapter implements ListAdapter {
  
      private ArrayList &lt; String &gt; productNames = new ArrayList &lt; &gt;();
      private ArrayList &lt; String &gt; productImages = new ArrayList &lt; &gt;();
      private Context context;
  
      public CustomProductListAdapter(ArrayList &lt; String &gt; productNames, ArrayList &lt; String &gt; productImages, Context context) {
          this.productNames = productNames;
          this.productImages = productImages;
          this.context = context;
      }
  
      @Override
      public int getCount() {
          return productNames.size();
      }
  
      @Override
      public Object getItem(int pos) {
          return productNames.get(pos);
      }
  
      @Override
      public long getItemId(int pos) {
          return 0;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          View view = convertView;
          if (view == null) {
              LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              view = inflater.inflate(R.layout.custom_product_list_layout, null);
          }
  
          final TextView listItemText = (TextView) view.findViewById(R.id.list_item_title);
  
          String productTitle = productNames.get(position);
          if (productNames.get(position).length() &gt; 25) productTitle = productTitle.substring(0, 22) + "...";
          listItemText.setText(productTitle);
  
          final ImageView productImg = (ImageView) view.findViewById(R.id.productImgView);
  
          Picasso.with(context).load(productImages.get(position)).into(productImg);
  
          return view;
      }
  }
  </code></pre>
  
  <p>This is Activity where I am loading and displaying the above image using Volley</p>
  
  <p><strong>ViewProductActivity Class</strong></p>
  
  <pre><code>public class ViewProductActivity extends AppCompatActivity {
  
      ListView productListView;
      String title = "exampleTitle";
  
      RequestQueue requestQueue;
      StringRequest request;
      final String PRODUCT_URL = "http://www.example.com/app/product.php";
  
      ArrayList &lt; String &gt; products,
      images;
      CustomProductListAdapter adapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_view_product);
  
          productListView = (ListView) findViewById(R.id.productListView);
          requestQueue = Volley.newRequestQueue(this);
          products = new ArrayList &lt; &gt;();
          images = new ArrayList &lt; &gt;();
  
          getProducts();
      }
  
      void getProducts() {
          request = new StringRequest(Request.Method.POST, PRODUCT_URL, new Response.Listener &lt; String &gt; () {@Override
              public void onResponse(String response) {
                  try {
                      JSONObject jsonObject = new JSONObject(response);
                      String viewName = jsonObject.getString("viewName");
                      JSONArray jsonArray = new JSONArray(viewName);
  
                      for (int x = 0; x &lt; jsonArray.length(); x++) {
                          JSONObject jsonPart = jsonArray.getJSONObject(x);
                          products.add(jsonPart.getString("name"));
                          images.add(jsonPart.getString("img"));
                      }
  
                      adapter = new CustomProductListAdapter(products, images, ViewProductActivity.this);
                      productListView.setAdapter(adapter);
  
                      progressBar.setVisibility(View.GONE);
  
                  } catch(JSONException e) {
                      e.printStackTrace();
                  }
              }
          },
          new Response.ErrorListener() {@Override
              public void onErrorResponse(VolleyError error) {
  
              }
          }) {@Override
              protected Map &lt; String,
              String &gt; getParams() throws AuthFailureError {
                  progressBar.setVisibility(View.VISIBLE);
                  HashMap &lt; String,
                  String &gt; hashMap = new HashMap &lt; &gt;();
                  hashMap.put("name", title);
                  return hashMap;
              }
          };
  
          requestQueue.add(request);
      }
  }
  </code></pre>
  
  <p>This is PHP script which queries the database and returns JSON</p>
  
  <p><strong>product.php</strong></p>
  
  <p>
  
  <pre><code>$con = mysqli_connect("localhost","username","password","db_name");
  
  // Check connection
  if (mysqli_connect_errno())
  {
      $json['failed'] = "Connection fail";
      echo json_encode($json);
  }
  
  $name = $_POST['name'];
  
  //fetch table rows from mysql db
  $sql = "SELECT name, img FROM table_name WHERE category = '$name'";
  $result = mysqli_query($con, $sql) or die("Error in Selecting");
  
  //create an array
  $emparray = array();
      while($row =mysqli_fetch_assoc($result))
  {
      $emparray[] = $row;
  }
  
  //echo json_encode($emparray);
  echo json_encode(array('viewName' =&gt; $emparray));
  
  //close the db connection
  mysqli_close($con);
  </code></pre>