How to stop requests get thrown with Volley within the fragments of a Drawer Menu?

0

Hello friends, I have a question. I have a Drawer menu in which I use various fragments to consult an internet service that is consumed with volley but when changing fragment without having finished loading that view the application collapses.

annex code

Flame code to the fragments from the drawer menu

 if (id == R.id.nav_resumen) {
            fm.beginTransaction().replace(R.id.frame, new ResumenFragment()).commit();
            setTitle("Resumen");
        } else if (id == R.id.nav_perfil) {
            fm.beginTransaction().replace(R.id.frame, new ProfileFragment()).commit();
            setTitle("Perfil");

        } else if (id == R.id.nav_asignadas) {

            //startActivity(new Intent(this, TerminarEnvioActivity.class));

            fm.beginTransaction().replace(R.id.frame, new AsignadasFragment()).commit();
            setTitle("Asignadas");

            // Toast.makeText(context, "Free", Toast.LENGTH_SHORT).show();

        } else if (id == R.id.nav_historial) {
            fm.beginTransaction().replace(R.id.frame, new HistorialFragment()).commit();
            setTitle("Historial");

Code that I use in each fragment to call my service with Volley

private void cargarDatos()
    {
        progressBar.setVisibility(View.VISIBLE);

        String urlBASE="https://" + UrlRemote.baseDebug + UrlRemote.EndPointProfile;

        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("driver_id",driver_id));
        params.add(new BasicNameValuePair("number", driver_id));
        NameValuePair Dato = SecurityUtils.getSignature(urlBASE, params);

        try {
            url2 = new URL(urlBASE + UrlRemote.getQuery(params));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String Firma = String.valueOf(Dato);

        String URLFINAL = url2+"&"+Firma;


        jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URLFINAL, null,this,this);
                jsonObjectRequest.setRetryPolicy(myRetryPolicy());
                VolleySingleton.getInstance(thiscontext).addToRequestQueue(jsonObjectRequest);
    }
    @Override
    public void onErrorResponse(VolleyError error) {
        progressBar.setVisibility(View.INVISIBLE);
        Toast.makeText(getActivity(), "No se puede optener los datos "+ error , Toast.LENGTH_SHORT).show();
        Log.i("error", error.toString());
    }

    @Override
    public void onResponse(JSONObject response) {
        progressBar.setVisibility(View.INVISIBLE);
        Profile profile = new Profile();
        JSONObject jsonObject = null;
        jsonObject = response.optJSONObject("Driver");
        profile.setBonus(jsonObject.optString("bonus"));
        profile.setCommissions(jsonObject.optString("commissions"));
        profile.setMonth(jsonObject.optString("month"));
        profile.setDriver_name(jsonObject.optString("Driver_name"));
        profile.setTotal(jsonObject.optString("total"));
        profile.setCycle(jsonObject.optString("cycle"));
        profile.setDriver_user(jsonObject.optString("Driver_user"));
        profile.setDriver_telephone(jsonObject.optString("Driver_telephone"));
        profile.setDriver_photo_url(jsonObject.optString("Driver_photo_url"));
        profile.setDiscounts(jsonObject.optString("discounts"));
        profile.setDriver_ranking(jsonObject.optString("Driver_ranking"));
        profile.setDairy(jsonObject.optString("dairy"));
        profile.setBalance(jsonObject.optString("balance"));
        profile.setDriver_plate(jsonObject.optString("Driver_plate"));
        profile.setPending(jsonObject.optString("pending"));


        //Vaciar los datos en los elementos
        Glide.with(getActivity())
                .load(profile.getDriver_photo_url())
                .into(circleImageView);
        nameDriver.setText(profile.getDriver_name().toString());
        userDriver.setText(profile.getDriver_user().toString());
        telefonoDriver.setText(profile.getDriver_telephone().toString());
        plateDriver.setText(profile.getDriver_plate().toString());
        ratingBar.setRating(Float.parseFloat(profile.getDriver_ranking()));

        //Vacias datos de sueldos del perfil
        textSueldo.setText(profile.getBalance().toString());
        textComisiones.setText(profile.getCommissions().toString());
        textBonos.setText(profile.getBonus());
        textDescuentos.setText(profile.getDiscounts());
        textPendientesPago.setText(profile.getPending());
        textTotal.setText(profile.getTotal());
        textEnviosDiarios.setText(profile.getDairy());
        textEnviosQuincenales.setText(profile.getCycle());
        textEnviosMensuales.setText(profile.getMonth());


    }

I hope and you can help me the error I get is:

  

java.lang.NullPointerException: Attempt to invoke virtual method   'android.content.res.Resources android.content.Context.getResources ()'   on a null object reference at   com.app99minutos.app99minutos.Views.ResumenFragment $ 3.onErrorResponse (SummaryFragment.java:258)

Every time I change the Fragment it remains making the previous request but as it has no where to print it collapses

    
asked by Alfredo Hernández 16.04.2018 в 23:57
source

1 answer

0

Actually it is not a problem to make several requests, the problem is that you should not print the value of error that is received within onErrorResponse() , the value of error , is of type VolleyError , here You must manage your own messages, for example:

@Override
public void onErrorResponse(VolleyError error) {

    if (error instanceof TimeoutError) {
        Toast.makeText(getActivity(), "Error Timeout", Toast.LENGTH_LONG).show();
    } else if (error instanceof NoConnectionError) {
        Toast.makeText(getActivity(), "Error Sin Conexión", Toast.LENGTH_LONG).show();
    } else if (error instanceof AuthFailureError) {
        Toast.makeText(getActivity(), "Error fallo al autenticar", Toast.LENGTH_LONG).show();
    } else if (error instanceof ServerError) {
        Toast.makeText(getActivity(), "Error servidor", Toast.LENGTH_LONG).show();
    } else if (error instanceof NetworkError) {
       Toast.makeText(getActivity(), "Error de Red", Toast.LENGTH_LONG).show();
    } else if (error instanceof ParseError) {
       Toast.makeText(getActivity(), "Error Parseo", Toast.LENGTH_LONG).show();
    }

}
    
answered by 17.04.2018 / 01:28
source