Volley file not downloaded using GSM
Asked Answered
C

1

9

I am using Volley to download a file from AWS. The file size is around 6 mb. In Android docs it is mentioned you should not download a large file using Volley. So what is the limit to the file size on Volley. I am using different network type and signal strength on emulator to check how my app behaves while downloading the file. I mainly want to check for GSM network type. If i keep Network type as GSM and even if my signal strength is great, i am getting timeout. If my Network Type is Full or LTE, the file gets downloaded very quickly. For GSM i even increased the retry limit to 5 minutes but still it gives me timeout. I dont want to allow the retry policy to be grater than 10 seconds. Here is my code.

public class MainActivity extends AppCompatActivity  {

    InputStreamVolleyRequest request;
    private TextView textView;
    private Button button;
    //private RequestQueue requestQueue;
    //WifiManager wifiManager;
     long FILE_SIZE ;
    long startTime;
    long endTime;
    private long takenTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv);
        button = findViewById(R.id.button);
        String mUrl="my url";
//        wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTime = System.currentTimeMillis();
                textView.setText("");


                MySingleton.getMySingleton(getApplicationContext()).addToRequestQueue(request);

//                requestQueue.add(request);
                button.setEnabled(false);

            }
        });

//        requestQueue = Volley.newRequestQueue(getApplicationContext());
        request = new InputStreamVolleyRequest(Request.Method.GET, mUrl, new Response.Listener<byte[]>() {
            @Override
            public void onResponse(byte[] response) {
                if(response!=null){
                    FileOutputStream outputStream;


                    String name = "justdownloaded.mp3";

                    try {

                        outputStream = openFileOutput(name, Context.MODE_PRIVATE);

                        outputStream.write(response);

                        outputStream.close();
                        Toast.makeText(MainActivity.this, "Download complete.", Toast.LENGTH_LONG).show();
                        textView.setText("Complete");

                        endTime = System.currentTimeMillis();


                        File file1 = new File(getApplicationContext().getFilesDir().getAbsolutePath(),name);
                        Log.i("TAG", "onResponse: "+MD5.calculateMD5(file1));
                        boolean b =MD5.checkMD5(request.responseHeaderETag,file1);
                        Log.i("TAG", "onResponse: "+b);
                        FILE_SIZE = file1.length();
                        int fileSize = (int) (FILE_SIZE / 1024);

                        takenTime = endTime - startTime;
                        double s = (double) takenTime / 1000;
                        double speed = fileSize / s;
                        Log.i("TAG", "onResponse: "+new DecimalFormat("##.##").format(speed)
                                + "kb/second"+fileSize);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    button.setEnabled(true);

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                if(error != null){
                    if(error.getClass().equals(TimeoutError.class)){
                        Toast.makeText(getApplicationContext(),"Time Out Error",Toast.LENGTH_LONG).show();
                    }
                }
                endTime = System.currentTimeMillis();

//                Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                Log.i("TAG", "onErrorResponse: "+error.networkResponse);
                Log.i("TAG", "onErrorResponse2: "+error.getLocalizedMessage());
                Log.i("TAG", "onErrorResponse3: "+error.getMessage());

                Log.i("TAG", "onErrorResponse4: "+error.getNetworkTimeMs());
                Log.i("TAG", "onErrorRespons5: "+error.getCause());
                Log.i("TAG", "onErrorRespons6: "+error.getStackTrace().toString());
                takenTime = endTime - startTime;
                button.setEnabled(true);
                textView.setText("Error");
                double s = (double) takenTime / 1000;
                double speed = FILE_SIZE / s;
                Log.i("TAG", "onResponse: "+new DecimalFormat("##.##").format(speed) + "kb/second");

            }
        });

        request.setRetryPolicy(new DefaultRetryPolicy(8000,
                2,
                2));

    }

}

All the log statements in the error listener returns null. I am able to see the TimeOut toast for GSM. Is it possible to do this for GSM using Volley instead of Download Manager? I can't use Retrofit as client's requirement is Volley What should i do differently for efficient download of file from server using GSM in minimum time

Chelsea answered 17/5, 2018 at 9:11 Comment(2)
Have you tried RetryPolicy? https://mcmap.net/q/126710/-change-volley-timeout-duration/7012517Blaylock
@Blaylock yes. I have mentioned in the code that I have set the retry policy to 5 minutesChelsea
G
1

If you want to download file within application with large size you can use https://github.com/amitshekhariitbhu/Fast-Android-Networking.

 public void downloadFile(String url) {

        AndroidNetworking.download(url, Utils.getRootDirPath(this), fileName)
                .setPriority(Priority.HIGH)
                .setMaxAgeCacheControl(1000, TimeUnit.DAYS)
                .build()
                .setDownloadProgressListener(new DownloadProgressListener() {
                    @Override
                    public void onProgress(long bytesDownloaded, long totalBytes) {
                        int per = (int) ((bytesDownloaded * 100) / totalBytes);

                    }
                })
                .startDownload(downloadListener);
    }
Gazo answered 25/5, 2018 at 11:21 Comment(2)
I have to use Volley, I can't use any other networking library as the project is almost to completion and changing the library can create unnecessary complications according to the clientChelsea
You only need to use new library ,where you are loading a Big files,for other all cases it's not need to change itGazo

© 2022 - 2024 — McMap. All rights reserved.