vitordelfino

Volley método POST

May 13th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. /* Método utilizado para fazer requisições POST ao webservice Enviando um objeto no formato Json */
  2. /* Enquanto a requisição esta sendo processada, aparece um Dialog de "carregando"*/
  3.  
  4. public void postJson(Context contexto, String url,final JSONObject jsonBody, final VolleyCallbackObject callback){
  5.         final RequestQueue request = Volley.newRequestQueue(contexto);
  6.         final JsonObjectRequest requisicao = new JsonObjectRequest(Request.Method.POST, url,jsonBody,
  7.                 new Response.Listener<JSONObject>(){
  8.                     @Override
  9.                     public void onResponse(JSONObject response) {
  10.                         callback.onSuccess(response);
  11.                         dialog.dismiss();
  12.                     }
  13.                 }, new Response.ErrorListener(){
  14.             @Override
  15.             public void onErrorResponse(VolleyError error ) {
  16.                 Log.i("ERROR", "onErrorResponse: " + error );
  17.                 dialog.dismiss();
  18.             }
  19.         }){
  20.             @Override
  21.             public Map<String, String> getHeaders() throws AuthFailureError {
  22.                 Map<String,String> params = new HashMap<String, String>();
  23.                 params.put("Content-Type","application/json");
  24.                 return params;
  25.             }
  26.         };
  27.  
  28.         int socketTimeout = 30000;//30 seconds - change to what you want
  29.         RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
  30.         requisicao.setRetryPolicy(policy);
  31.         request.add(requisicao);
  32.         dialog = new ProgressDialog(contexto);
  33.         dialog.setMessage("Carregando....");
  34.         dialog.show();
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment