Android Volley Double Post

时间:2016-11-04 04:33:20

标签: android android-volley

它运行两次;它应该只运行一次。我试过

_urlWebService = vars.ipServer.concat("/ws/disponibilidadTicket");

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, _urlWebService, null,
        new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                try
                {
                    JSONArray clientes;

                    if(response.getBoolean("status"))
                    {


                    }

                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        },

        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {

                if (error instanceof TimeoutError)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder
                            .setMessage("Error de conexión, sin respuesta del servidor.")
                            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int id)
                                {
                                }
                            }).show();
                }

                else

                if (error instanceof NoConnectionError)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder
                            .setMessage("Por favor, conectese a la red.")
                            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int id)
                                {
                                }
                            }).show();
                }

                else

                if (error instanceof AuthFailureError)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder
                            .setMessage("Error de autentificación en la red, favor contacte a su proveedor de servicios.")
                            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int id)
                                {
                                }
                            }).show();
                }

                else

                if (error instanceof ServerError)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder
                            .setMessage("Error server, sin respuesta del servidor.")
                            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int id)
                                {
                                }
                            }).show();
                }

                else

                if (error instanceof NetworkError)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder
                            .setMessage("Error de red, contacte a su proveedor de servicios.")
                            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int id)
                                {
                                }
                            }).show();
                }

                else

                if (error instanceof ParseError)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                    builder
                            .setMessage("Error de conversión Parser, contacte a su proveedor de servicios.")
                            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int id)
                                {
                                }
                            }).show();
                }

            }

        })
{
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError
    {
        HashMap<String, String> headers = new HashMap <String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        headers.put("WWW-Authenticate", "xBasic realm=".concat(""));
        headers.put("MyToken",sharedPreferences.getString("MyTokenAPI"));
        return headers;
    }
};

ControllerSingleton.getInstance().addToReqQueue(jsonObjReq, "");
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(20000, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
 }

然而它跑了两次。有人知道如何处理这个问题,因为它应该只运行一次。

1 个答案:

答案 0 :(得分:0)

尝试传递-1而不是向retryPolicy发送1,而不是只调用一次代码。这样的事情: -

    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(20000, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

希望,它解决了你的问题。