排球错误响应无效

时间:2018-05-14 14:35:54

标签: android android-volley

我正在从服务器下载文件。我正在使用Volley这样做。

这是我的代码

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



    }

}

以下是错误侦听器的错误消息 在一些弱的网络连接上,我得到以下逻辑消息

onErrorResponse: null
onErrorResponse2: null
onErrorResponse3: null
onErrorResponse4: 25807
onErrorRespons5: null
onErrorRespons6: [Ljava.lang.StackTraceElement;@14b4f28

我不明白失败的原因是什么,因为大多数错误消息都返回null。我很确定这是因为请求超时并且我知道如何增加超时,但我应该在错误监听器中编写什么代码,以便在出现错误时可以获得正确的错误消息。

1 个答案:

答案 0 :(得分:0)

这个

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

应该是

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

首先解决这个问题!