[英]Volley Error Response Gives Null
I am downloading a file from a server. 我正在从服务器下载文件。 I am using Volley to do so. 我正在使用Volley来这样做。
Here is my code 这是我的代码
public class MainActivity extends AppCompatActivity {
InputStreamVolleyRequest request;
private TextView textView;
private Button button;
private RequestQueue requestQueue;
WifiManager wifiManager;
@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(Context.WIFI_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("");
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 {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int speedMbps = wifiInfo.getLinkSpeed();
Log.i("Speed", "onResponse: "+speedMbps);
outputStream = openFileOutput(name, Context.MODE_PRIVATE);
outputStream.write(response);
Log.i("TAG", "onResponse: "+ Arrays.toString(response));
outputStream.close();
Toast.makeText(MainActivity.this, "Download complete.", Toast.LENGTH_LONG).show();
textView.setText("Complete");
// Log.i("TAG", "onResponse: "+getApplicationContext().getFilesDir().getAbsolutePath());
} 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();
}
}
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());
button.setEnabled(true);
textView.setText("Error");
}
});
}
}
Following are the error messages of error listener On some weak network connection I get the following logical messages 以下是错误侦听器的错误消息在某些弱网络连接上,我收到以下逻辑消息
onErrorResponse: null
onErrorResponse2: null
onErrorResponse3: null
onErrorResponse4: 25807
onErrorRespons5: null
onErrorRespons6: [Ljava.lang.StackTraceElement;@14b4f28
I don't understand what is the reason of the failure as most of the error messages returns null. 我不明白失败的原因是什么,因为大多数错误消息都返回null。 I am quite sure it is because of request time out and I know how to increase the timeout, but what code should I write in the error listener so that I can get the correct error message if there is an error. 我很确定这是因为请求超时,并且我知道如何增加超时,但是我应该在错误侦听器中编写什么代码,以便在出现错误时得到正确的错误消息。
This 这个
if (error == null) {
if (error.getClass().equals(TimeoutError.class)){
Toast.makeText(getApplicationContext(),"Time Out Error",Toast.LENGTH_LONG).show();
}
}
Should be 应该
if (error != null) {
if (error.getClass().equals(TimeoutError.class)) {
Toast.makeText(getApplicationContext(),"Time Out Error",Toast.LENGTH_LONG).show();
}
}
first fix this! 首先解决这个!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.