未使用GSM

时间:2018-05-17 09:11:25

标签: android android-volley

我正在使用Volley从AWS下载文件。文件大小约为6 MB。在Android文档中提到您不应该使用Volley下载大文件。那么Volley的文件大小限制是多少? 我在模拟器上使用不同的网络类型和信号强度来检查我的应用程序在下载文件时的行为方式。我主要想检查GSM网络类型。如果我保持网络类型为GSM,即使我的信号强度很大,我也会超时。如果我的网络类型为Full或LTE,则文件下载速度非常快。对于GSM我甚至将重试限制增加到5分钟,但它仍然给我超时。我不想让重试政策大于10秒。这是我的代码。

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));

    }

}

错误侦听器中的所有日志语句都返回null。我能看到GSM的TimeOut吐司。是否可以使用Volley而不是Download Manager为GSM执行此操作? 我无法使用Retrofit,因为客户的要求是Volley 在最短时间内使用GSM从服务器高效下载文件我应该做些什么呢?

1 个答案:

答案 0 :(得分:1)

如果要在大尺寸应用程序中下载文件,可以使用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);
    }