代码之家  ›  专栏  ›  技术社区  ›  user5434198

截击错误响应为空

  •  0
  • user5434198  · 技术社区  · 7 年前

    我正在从服务器下载文件。我用截击来做到这一点。

    这是我的密码

    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 回复  |  直到 7 年前
        1
  •  0
  •   ColdFire    7 年前

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

    首先解决这个问题!