Stephen
Stephen

Reputation: 10059

call a method several times before getting response from volley

I am getting base 64 encoded format image string from server: hNiCHLy9Ax3gKMv81IBJ1457175735.So I downloaded and use that imagestring to covert that into bitmap.

Now My only problem is, I have to call the loadImage(base64Image) method nearly eight times inside that for loop.But it is not calling the method single time because of asynchronous volley, it directly get into volley get response.

Because of that I can't save values to arraylist.that's why I am getting arrayIndexOutOfBoundException.

Below I have posted the logcat and codes relevant to this issue:

Logcat: (Edited)

03-22 08:39:43.795 30971-30971/com.android.burblr E/urlAva: http://example.com/api/v1/file=3UulRIaeyVTpKYfkA5og1458204300
03-22 08:39:43.795 30971-30971/com.android.burblr E/base64Image: 3UulRIaeyVTpKYfkA5og1458204300
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling BitMAp....: 3UulRIaeyVTpKYfkA5og1458204300
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling try: Calling try
03-22 08:39:43.795 30971-30971/com.android.burblr E/getAvaArrStr: cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/urlAva: http://example.com/api/v1/file=cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/base64Image: cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling BitMAp....: cUZdHWN9KzYMtmusvqnE1446770662
03-22 08:39:43.795 30971-30971/com.android.burblr E/Calling try: Calling try
03-22 08:39:43.805 30971-30971/com.android.burblr D/Volley: [1] DiskBasedCache.remove: Could not delete cache entry for key=http://example.com/api/v1/file=cUZdHWN9KzYMtmusvqnE1446770662, filename=993968043-2098536571
03-22 08:39:43.827 30971-31011/com.android.burblr W/EGL_emulation: eglSurfaceAttrib not implemented
03-22 08:39:43.827 30971-31011/com.android.burblr W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe195f3c0, error=EGL_SUCCESS
03-22 08:39:44.088 30971-30971/com.android.burblr E/came: came
03-22 08:39:44.088 30971-30971/com.android.burblr E/arrBitMap: []

CardFragment.java:

  void hitImageApi(){

        final ProgressDialog dialog = ProgressDialog.show(getActivity(), null, null);
        ProgressBar spinner = new android.widget.ProgressBar( getActivity(),null,android.R.attr.progressBarStyle);
        spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(spinner);
        dialog.setCancelable(false);
        dialog.show();



        for (int i=0; i < alAvaArr.size(); i++){

             getAvaArrStr = alAvaArr.get(i);

            Log.e("getAvaArrStr",""+getAvaArrStr);


            urlAva = BurblrUtils.BR_AVATAR_IMAGE + "file=" +getAvaArrStr ;

            Log.e("urlAva", urlAva);

            base64Image = getAvaArrStr;

            Log.e("base64Image", "" + base64Image);

            loadImage(base64Image);


            requestAva = new StringRequest(Request.Method.GET, urlAva, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    if(response != null && !response.startsWith("<HTML>")){
                        Log.e("ResponseAvatar", response);
                        dialog.dismiss();

                        try {

                            Toast.makeText(getActivity(), "Running ", Toast.LENGTH_SHORT).show();

                            getSwipeImage();

                            myAppAdapter.notifyDataSetChanged();

                        } catch (Exception e) {
                            e.printStackTrace();
                            dialog.dismiss();
                        }

                    }else{
                        dialog.dismiss();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    if(error != null){
                        Log.e("error", error.toString());
                        dialog.dismiss();
                    }

                }
            }){
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();

                    params.put("file", getAvaArrStr );

                    Log.e("paramsImg", ""+params);

                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("Content-Type","application/x-www-form-urlencoded");
                    return params;
                }
            };

        }

        RequestQueue queue = Volley.newRequestQueue(getActivity());
        queue.add(requestAva);
        queue.getCache().remove(urlAva);

    }





      void loadImage(String src) {

     Log.e("Calling BitMAp....", "" + src);


     try {

         Log.e("Calling try", "Calling try");
         URL url = new URL(src);

         Log.e("url", ""+url);

         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
         connection.setDoInput(true);
         connection.connect();
         InputStream input = connection.getInputStream();
         Bitmap myBitmap = BitmapFactory.decodeStream(input);

         Log.e("myBitmap", ""+myBitmap);

         bitMapToString(myBitmap);

     } catch (IOException e) {
         // Log exception
         Log.e("IOError", ""+e);
     }

}

    public String bitMapToString(Bitmap bitmap){


        ByteArrayOutputStream baos=new  ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
        byte [] b=baos.toByteArray();
        String temp=Base64.encodeToString(b, Base64.DEFAULT);

        Log.e("temp", ""+temp);

        arrBitMap.add(temp);

        return temp;
    }

I need to call volley GET response after called loadImage(base64Image) method eight times.any suggestion

Upvotes: 0

Views: 166

Answers (1)

Mansha Chuttani
Mansha Chuttani

Reputation: 381

So basically u want to avoid all the work you are doing in onResponse till the load image is called the number of times equal to the size of alAvaArr array. So, you can declare a global int variable up and increment it each time in onResponse. Now check in on response that the value of this variable is equal to the size of alAvaArr array or not. If not, do nothing else whatever you wana do in OnResponse:

                    dialog.dismiss();

                    try {

                        Toast.makeText(getActivity(), "Running ", Toast.LENGTH_SHORT).show();

                        getSwipeImage();

                        myAppAdapter.notifyDataSetChanged();

                    } catch (Exception e) {
                        e.printStackTrace();
                        dialog.dismiss();
                    }

Upvotes: 1

Related Questions