【发布时间】:2018-10-27 11:53:08
【问题描述】:
我正在使用 Volley 从 AWS 下载文件。文件大小约为 6 MB。在 Android 文档中提到您不应该使用 Volley 下载大文件。那么 Volley 上文件大小的限制是多少。 我在模拟器上使用不同的网络类型和信号强度来检查我的应用程序在下载文件时的行为方式。我主要想检查 GSM 网络类型。如果我将网络类型保持为 GSM,即使我的信号强度很好,我也会超时。如果我的网络类型是完整或 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 而不是下载管理器为 GSM 执行此操作? 我不能使用改造,因为客户的要求是 Volley 为了在最短的时间内使用 GSM 从服务器高效下载文件,我应该做些什么不同的事情
【问题讨论】:
-
您尝试过 RetryPolicy 吗? stackoverflow.com/q/17094718/7012517
-
@Shobhit 是的。我在代码中提到我已将重试策略设置为 5 分钟